Class: Handlebarer::Compiler
Instance Method Summary collapse
-
#compile(template) ⇒ String
Compile a Handlerbars template for client-side use with JST.
-
#handlebars_helpers ⇒ Array<String>
Handlebars helpers.
-
#handlebars_version ⇒ String
Handlerbars Javascript engine version.
-
#render(template, vars = {}) ⇒ String
Compile and evaluate a Handlerbars template for server-side rendering.
-
#source ⇒ String
Handlerbars template engine Javascript source code used to compile templates in ExecJS.
-
#v8_context {|context| ... } ⇒ Object
V8 context with Handlerbars code compiled.
Instance Method Details
#compile(template) ⇒ String
Compile a Handlerbars template for client-side use with 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) = context.eval("Handlebars.precompile(#{template.to_json})") "Handlebars.template(#{});" end end |
#handlebars_helpers ⇒ Array<String>
Handlebars helpers
58 59 60 61 62 63 64 65 66 |
# File 'lib/handlebarer/compiler.rb', line 58 def 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_version ⇒ String
Handlerbars Javascript engine version
24 25 26 27 28 |
# File 'lib/handlebarer/compiler.rb', line 24 def v8_context do |context| context.eval("Handlebars.VERSION") end end |
#render(template, vars = {}) ⇒ String
Compile and evaluate a Handlerbars template for server-side rendering
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 = 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 |
#source ⇒ String
Handlerbars template engine Javascript source code used to compile templates in ExecJS
8 9 10 |
# File 'lib/handlebarer/compiler.rb', line 8 def source @source ||= Handlebarer::Source:: end |
#v8_context {|context| ... } ⇒ Object
V8 context with Handlerbars code compiled
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 |