Class: Dolt::TemplateRenderer

Inherits:
Object
  • Object
show all
Defined in:
lib/dolt/template_renderer.rb

Instance Method Summary collapse

Constructor Details

#initialize(template_root, opt = {}) ⇒ TemplateRenderer

Returns a new instance of TemplateRenderer.



22
23
24
25
26
27
28
# File 'lib/dolt/template_renderer.rb', line 22

def initialize(template_root, opt = {})
  @template_root = template_root
  @cache = {} if !opt.key?(:cache) || opt[:cache]
  @layout = opt[:layout]
  @type = opt[:type] || "erb"
  @context_class = Class.new
end

Instance Method Details

#helper(helper) ⇒ Object



30
31
32
33
# File 'lib/dolt/template_renderer.rb', line 30

def helper(helper)
  helper = [helper] unless Array === helper
  helper.each { |h| @context_class.send(:include, h) }
end

#render(template, locals = {}, options = {}) ⇒ Object



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

def render(template, locals = {}, options = {})
  context = context_class.new
  content = load(template).render(context, locals)
  layout_tpl = options.key?(:layout) ? options[:layout] : layout

  if !layout_tpl.nil?
    content = load(layout_tpl).render(context, locals) { content }
  end

  content
end