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
# 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

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

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

#rendering_engineObject



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

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

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

#scope(context) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/dimples/renderable.rb', line 23

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

    method_name = :render_template
    scope.class.send(:define_method, method_name) do |template, locals = {}|
      @site.templates[template]&.render(locals)
    end
  end
end