Class: Dimples::Renderer

Inherits:
Object
  • Object
show all
Defined in:
lib/dimples/renderer.rb

Overview

A wrapper around Tilt, tied to a single post, page or template.

Instance Method Summary collapse

Constructor Details

#initialize(site, source) ⇒ Renderer

Returns a new instance of Renderer.



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

def initialize(site, source)
  @site = site
  @source = source
end

Instance Method Details

#engineObject



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

def engine
  @engine ||= begin
    callback = proc { @source.contents }

    if @source.path
      extension = File.extname(@source.path)
      options = @site.config.rendering[extension.to_sym] || {}

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

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



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/dimples/renderer.rb', line 11

def render(context = {}, body = nil)
  context[:site] ||= @site
  context[:pagination] ||= nil

  output = engine.render(scope, context) { body }.strip
  @source.[:rendered_contents] = output

  if @source.[:layout]
    template = @site.templates[@source.[:layout]]
  end

  return output if template.nil?

  template.render(context, output)
end

#scopeObject



42
43
44
45
46
47
48
49
50
# File 'lib/dimples/renderer.rb', line 42

def scope
  @scope ||= Object.new.tap do |scope|
    scope.instance_variable_set(:@site, @site)

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