Module: Dimples::Renderable

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

Instance Method Summary collapse

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.message)
  end

  if use_layout && defined?(@layout) && @site.templates[@layout]
    output = @site.templates[@layout].render(context, output)
  end

  output
end

#rendererObject



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]
    options = @site.config['rendering'][extension] || {}

    Tilt.new(@path, options, &callback)
  else
    Tilt::StringTemplate.new(&callback)
  end
end