Class: Rory::Renderer
- Inherits:
-
Object
- Object
- Rory::Renderer
- Defined in:
- lib/rory/renderer.rb,
lib/rory/renderer/context.rb
Defined Under Namespace
Classes: Context
Instance Method Summary collapse
-
#initialize(template_name, options = {}) ⇒ Renderer
constructor
A new instance of Renderer.
- #layout_path ⇒ Object
- #render(&block) ⇒ Object
- #view_binding ⇒ Object
- #view_path ⇒ Object
- #view_template ⇒ Object
Constructor Details
#initialize(template_name, options = {}) ⇒ Renderer
Returns a new instance of Renderer.
6 7 8 9 10 |
# File 'lib/rory/renderer.rb', line 6 def initialize(template_name, = {}) @template_name = template_name @options = @app = [:app] end |
Instance Method Details
#layout_path ⇒ Object
33 34 35 36 |
# File 'lib/rory/renderer.rb', line 33 def layout_path return nil unless @options[:layout] File.join('layouts', @options[:layout].to_s) end |
#render(&block) ⇒ Object
12 13 14 15 16 17 18 19 20 |
# File 'lib/rory/renderer.rb', line 12 def render(&block) erb = ERB.new(view_template) output = erb.result(view_binding(&block)) if layout_path layout_renderer = self.class.new(layout_path, @options.merge(:layout => false)) output = layout_renderer.render { output } end output end |
#view_binding ⇒ Object
22 23 24 25 26 27 |
# File 'lib/rory/renderer.rb', line 22 def view_binding render_context = Context.new(@options) render_context.get_binding { |*args| yield(args) if block_given? } end |
#view_path ⇒ Object
38 39 40 41 |
# File 'lib/rory/renderer.rb', line 38 def view_path root = @app ? @app.root : Rory.root File.(File.join('views', "#{@template_name}.html.erb"), root) end |
#view_template ⇒ Object
29 30 31 |
# File 'lib/rory/renderer.rb', line 29 def view_template File.read(view_path) end |