Module: Dimples::Renderable

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

Instance Method Summary collapse

Instance Method Details

#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
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/dimples/renderable.rb', line 3

def render(context = {}, body = nil, use_layout = true)
  class_name = self.class.name.split('::').last.downcase.to_sym

  context[:site] = @site unless context[:site]
  context[class_name] = self unless context[class_name]

  scope = Object.new

  context.each_pair do |key, value|
    scope.instance_variable_set("@#{key}".to_sym, value)
  end

  begin
    output = renderer.render(scope) { body }.strip
    @rendered_contents = output
  rescue RuntimeError, TypeError, NoMethodError, SyntaxError, NameError => e
    problem_file = if @path
      @path.gsub(@site.source_paths[:root], '')
    else
      "dynamic #{self.class}"
    end

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

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

  output
end

#rendererObject



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/dimples/renderable.rb', line 35

def renderer
  proc = Proc.new { |template| contents() }

  if @path
    extension = File.extname(@path)[1..-1]
    options = @site.config['rendering'][extension] || {}

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