Module: Handlebarer::Renderer

Defined in:
lib/handlebarer/renderer.rb

Overview

Server side Jade templates renderer

Class Method Summary collapse

Class Method Details

.call(template) ⇒ Object

Prepare controller instance variables for the template and execute template conversion. Called as an ActionView::Template registered template

Parameters:

  • template (ActionView::Template)

    currently rendered ActionView::Template instance

See Also:

  • #convert_template


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

def self.call(template)
  #template.source.gsub!(/\#\{([^\}]+)\}/,"\\\#{\\1}") # escape Handlebars' #{somevariable} syntax
  %{
    template_source = %{#{template.source}}
    variable_names = controller.instance_variable_names
    variable_names -= %w[@template]
    if controller.respond_to?(:protected_instance_variables)
      variable_names -= controller.protected_instance_variables
    end

    variables = {}
    variable_names.each do |name|
      next if name.include? '@_'
      variables[name.sub(/^@/, "")] = controller.instance_variable_get(name)
    end
    Handlebarer::Renderer.convert_template(template_source, variables.merge(local_assigns))
  }
end

.convert_template(template_text, vars = {}) ⇒ String

Convert Handlebars template to HTML output for rendering as a Rails view

Parameters:

  • template_text (String)

    Handlebars template text to convert

  • controller_name (String)

    name of Rails controller rendering the view

  • vars (Hash) (defaults to: {})

    controller instance variables passed to the template

Returns:

  • (String)

    HTML output of evaluated template

See Also:



11
12
13
14
# File 'lib/handlebarer/renderer.rb', line 11

def self.convert_template(template_text, vars = {})
  compiler = Handlebarer::Compiler.new
  compiler.render(template_text, vars)
end