Sha256: f2f50672b5736a38f4481f9876ef5d7ad2e8046fb345e98b94b395d861c4a2ac

Contents?: true

Size: 965 Bytes

Versions: 3

Compression:

Stored size: 965 Bytes

Contents

module Mutton
  module HandlebarsCompiler
    module_function

    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

    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

    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

    # TODO: get from asset pipeline
    def handlebars_source
      Pathname(Mutton.handlebars_file).read
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
mutton-0.0.12 lib/mutton/handlebars_compiler.rb
mutton-0.0.11 lib/mutton/handlebars_compiler.rb
mutton-0.0.10 lib/mutton/handlebars_compiler.rb