Class: Handlebarer::Compiler

Inherits:
Object
  • Object
show all
Defined in:
lib/handlebarer/compiler.rb

Instance Method Summary collapse

Instance Method Details

#compile(template) ⇒ String

Compile a Handlerbars template for client-side use with JST

Parameters:

  • template (String, File)

    Handlerbars template file or text to compile

Returns:

  • (String)

    Handlerbars template compiled into Javascript and wrapped inside an anonymous function for JST



33
34
35
36
37
38
39
# File 'lib/handlebarer/compiler.rb', line 33

def compile(template)
  v8_context do |context|
    template = template.read if template.respond_to?(:read)
    compiled_handlebars = context.eval("Handlebars.precompile(#{template.to_json})")
    "Handlebars.template(#{compiled_handlebars});"
  end
end

#handlebars_helpersArray<String>

Handlebars helpers

Returns:

  • (Array<String>)

    array of Handlebars helpers to use with a Handlebars template rendered by a Rails controller



58
59
60
61
62
63
64
65
66
# File 'lib/handlebarer/compiler.rb', line 58

def handlebars_helpers
  helpers = []
  unless Handlebarer.configuration.helpers_path.nil?
    Dir["#{Handlebarer.configuration.helpers_path}/*.js"].each do |f|
      helpers << IO.read(f)
    end
  end
  helpers
end

#handlebars_versionString

Handlerbars Javascript engine version

Returns:

  • (String)

    version of Handlerbars javascript engine installed in ‘vendor/assets/javascripts`



24
25
26
27
28
# File 'lib/handlebarer/compiler.rb', line 24

def handlebars_version
  v8_context do |context|
    context.eval("Handlebars.VERSION")
  end
end

#render(template, vars = {}) ⇒ String

Compile and evaluate a Handlerbars template for server-side rendering

Parameters:

  • template (String)

    Handlerbars template text to render

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

    controller instance variables passed to the template

Returns:

  • (String)

    HTML output of compiled Handlerbars template



45
46
47
48
49
50
51
52
53
54
# File 'lib/handlebarer/compiler.rb', line 45

def render(template, vars = {})
  v8_context do |context|
    unless Handlebarer.configuration.nil?
      helpers = handlebars_helpers
      context.eval(helpers.join("\n")) if helpers.any?
    end
    context.eval("var fn = Handlebars.compile(#{template.to_json})")
    context.eval("fn(#{vars.to_hbs.to_json})")
  end
end

#sourceString

Handlerbars template engine Javascript source code used to compile templates in ExecJS

Returns:

  • (String)

    Handlerbars source code



8
9
10
# File 'lib/handlebarer/compiler.rb', line 8

def source
  @source ||= Handlebarer::Source::handlebars
end

#v8_context {|context| ... } ⇒ Object

V8 context with Handlerbars code compiled

Yields:

  • (context)

    V8::Context compiled Handlerbars source code in V8 context



14
15
16
17
18
19
20
# File 'lib/handlebarer/compiler.rb', line 14

def v8_context
  V8::C::Locker() do
    context = V8::Context.new
    context.eval(source)
    yield context
  end
end