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



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}"

    error_message = "Unable to render #{file}"
    error_message << " (#{e.message})" if @site.config['verbose_logging']

    raise Errors::RenderingError.new(e.message)
  end

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

  output
end

#rendererObject



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

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