Class: Dimples::Renderer

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

Instance Method Summary collapse

Constructor Details

#initialize(site, source) ⇒ Renderer

Returns a new instance of Renderer.



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

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

Instance Method Details

#engineObject



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

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



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

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

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

  template = @site.templates[@source.[:layout]]
  return output if template.nil?

  template.render(context, output)
end

#scopeObject



38
39
40
41
42
43
44
45
46
# File 'lib/dimples/renderer.rb', line 38

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