Module: Dimples::Renderable
Instance Method Summary collapse
- #build_scope(context) ⇒ Object
- #render(context = {}, body = nil, use_layout = true) ⇒ Object
- #renderer ⇒ Object
Instance Method Details
#build_scope(context) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/dimples/renderable.rb', line 19 def build_scope(context) context[:site] ||= @site context[:this] ||= self context[:type] ||= self.class.name.split('::').last.downcase.to_sym scope = Object.new context.each_pair do |key, value| scope.instance_variable_set("@#{key}".to_sym, value) end scope end |
#render(context = {}, body = nil, use_layout = true) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/dimples/renderable.rb', line 3 def render(context = {}, body = nil, use_layout = true) begin output = renderer.render(build_scope(context)) { body }.strip @rendered_contents = output rescue RuntimeError, TypeError, NoMethodError, SyntaxError, NameError => e file = @path || "dynamic #{self.class}" raise Errors::RenderingError.new(file, e.) end if use_layout && defined?(@layout) && @site.templates[@layout] output = @site.templates[@layout].render(context, output) end output end |
#renderer ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/dimples/renderable.rb', line 33 def renderer callback = proc { contents } if @path extension = File.extname(@path)[1..-1] = @site.config['rendering'][extension] || {} Tilt.new(@path, , &callback) else Tilt::StringTemplate.new(&callback) end end |