Class: Ree::TemplateRenderer

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

Constant Summary collapse

TEMPLATE_VARIABLE =
/<%=\s*([\w\d-]+)\s*%>/i

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template, locals) ⇒ TemplateRenderer

Returns a new instance of TemplateRenderer.



14
15
16
17
18
# File 'lib/ree/templates/template_renderer.rb', line 14

def initialize(template, locals)
  @template = template
  @locals   = locals
  @undefined_variables = []
end

Class Method Details

.get_undefined_variables(template, locals = {}) ⇒ Object



9
10
11
# File 'lib/ree/templates/template_renderer.rb', line 9

def get_undefined_variables(template, locals = {})
  Ree::TemplateRenderer.new(template, locals).get_undefined_variables
end

.handle(template, locals = {}) ⇒ Object



5
6
7
# File 'lib/ree/templates/template_renderer.rb', line 5

def handle(template, locals = {})
  Ree::TemplateRenderer.new(template, locals).handle
end

Instance Method Details

#get_undefined_variablesObject



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/ree/templates/template_renderer.rb', line 26

def get_undefined_variables
  Ree::RenderUtils.render(@template, @locals)

  @undefined_variables
rescue NameError => e
  raise NoMethodError, "Undefined method for template. Please, add :#{e.name} method to .ree/helpers/render_helper.rb file!" if @undefined_variables.include?(e.name)

  @locals[e.name] = e.name.to_s
  @undefined_variables.push(e.name)

  retry
end

#handleObject

Raises:



20
21
22
23
24
# File 'lib/ree/templates/template_renderer.rb', line 20

def handle
  raise Ree::Error.new("Template variable not defined: #{get_undefined_variables.map(&:to_s).join(';')}") if get_undefined_variables.any?

  Ree::RenderUtils.render(@template, @locals)
end