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
23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/dimples/renderable.rb', line 23 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 18 19 20 21 |
# 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 || "a dynamic #{self.class}" = "Unable to render #{file}" << " (#{e.})" if @site.config['verbose_logging'] raise Errors::RenderingError.new(e.) end if use_layout && defined?(@layout) && @site.templates[@layout] output = @site.templates[@layout].render(context, output) end output end |
#renderer ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/dimples/renderable.rb', line 37 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 |