Module: Mutton::HandlebarsCompiler

Defined in:
lib/mutton/handlebars_compiler.rb

Class Method Summary collapse

Class Method Details

.compile_handlebars(template_content, template_namespace, mutton_namespace, javascript_helpers = nil) ⇒ Object



11
12
13
# File 'lib/mutton/handlebars_compiler.rb', line 11

def compile_handlebars(template_content, template_namespace, mutton_namespace, javascript_helpers = nil)
  ExecJS.compile("#{handlebars_source}; #{javascript_helpers}; #{template_content}; var template =  this.#{mutton_namespace}['#{template_namespace}'];")
end

.handlebars_sourceObject

TODO: get from asset pipeline



21
22
23
# File 'lib/mutton/handlebars_compiler.rb', line 21

def handlebars_source
  Pathname(Mutton.handlebars_file).read
end

.pre_compile(source) ⇒ Object



5
6
7
8
9
# File 'lib/mutton/handlebars_compiler.rb', line 5

def pre_compile(source)
  source = source.read if source.respond_to?(:read)
  handlebar_context = ExecJS.compile(handlebars_source)
  handlebar_context.call('Handlebars.precompile', source)
end

.process_handlebars(template_content, template_namespace, mutton_namespace, assigns, javascript_helpers = nil) ⇒ Object



15
16
17
18
# File 'lib/mutton/handlebars_compiler.rb', line 15

def process_handlebars(template_content, template_namespace, mutton_namespace, assigns, javascript_helpers = nil)
  compiled = compile_handlebars(template_content, template_namespace, mutton_namespace, javascript_helpers)
  compiled.call('template', assigns.as_json)
end