Class: Magneto::RenderContext

Inherits:
Context
  • Object
show all
Defined in:
lib/magneto/render_context.rb

Overview

Adapted from ‘nanoc/helpers/rendering.rb` and `nanoc/helpers/capturing.rb` with thanks to Denis Defreyne and contributors.

Instance Method Summary collapse

Methods inherited from Context

#get_binding, #initialize

Constructor Details

This class inherits a constructor from Magneto::Context

Instance Method Details

#render(template_name, args = {}, &block) ⇒ Object



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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/magneto/render_context.rb', line 7

def render(template_name, args = {}, &block)
  template = @site.templates[template_name.to_sym]

  if template.nil?
    $stderr.puts "#{File.basename($PROGRAM_NAME)}: #{ex.to_s}"
    raise "Couldn't find template: '#{template_name.to_s}'"
  end

  if block_given?
    # Get templating system output instance.
    erbout = eval('_erbout', block.binding)

    # Save output length.
    erbout_length = erbout.length

    # Execute block (which may cause recursion).
    block.call

    # Use additional output from block execution as content.
    current_content = erbout[erbout_length..-1]

    # Remove addition from templating system output.
    erbout[erbout_length..-1] = ''
  else
    current_content = nil
  end

  ivars = {}
  self.instance_variables.each { |ivar| ivars[ivar[1..-1].to_sym] = self.instance_variable_get(ivar) }

  result = template.filter.apply(template.content, ivars.deep_merge({
    :content => current_content
  }).deep_merge(template. || {}).deep_merge(args.symbolize_keys))

  if block_given?
    # Append filter result to templating system output and return empty
    # string.
    erbout << result
    ''
  else
    result
  end
end