Module: Dimples::Renderable

Included in:
Page, Template
Defined in:
lib/dimples/renderable.rb

Overview

A mixin class that allows a document to render via Tilt.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#rendered_contentsObject

Returns the value of attribute rendered_contents.



6
7
8
# File 'lib/dimples/renderable.rb', line 6

def rendered_contents
  @rendered_contents
end

Instance Method Details

#render(context = {}, body = nil) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/dimples/renderable.rb', line 8

def render(context = {}, body = nil)
  context[:site] ||= @site
  context[:this] ||= self
  context[:type] ||= self.class.name.split('::').last.downcase.to_sym

  scope = Object.new.tap do |s|
    context.each_pair do |key, value|
      s.instance_variable_set("@#{key}".to_sym, value)
    end
  end

  output = rendering_engine.render(scope) { body }.strip
  @rendered_contents = output

  if @site.templates[layout]
    @site.templates[layout].render(context, output)
  else
    output
  end
end

#rendering_engineObject



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/dimples/renderable.rb', line 29

def rendering_engine
  @rendering_engine ||= begin
    callback = proc { contents }

    if @path
      ext = File.extname(@path)[1..-1]
      options = @site.config['rendering'][ext] || {}
      Tilt.new(@path, options, &callback)
    else
      Tilt::StringTemplate.new(&callback)
    end
  end
end