Module: Dimples::Renderable
Overview
A mixin class that handles rendering via a Tilt template.
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
22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/dimples/renderable.rb', line 22 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
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/dimples/renderable.rb', line 6 def render(context = {}, body = nil, use_layout = true) output = renderer.render(build_scope(context)) { body }.strip @rendered_contents = output if use_layout && defined?(@layout) && @site.templates[@layout] output = @site.templates[@layout].render(context, output) end output rescue => e error_name = e.class.to_s.gsub(/([A-Z])/, ' \\1').strip.downcase = "Unable to render #{@path || self.class} (#{error_name})" raise Errors::RenderingError.new, end |
#renderer ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/dimples/renderable.rb', line 36 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 |