Class: Handlebars::Engine
- Inherits:
-
Object
- Object
- Handlebars::Engine
- Defined in:
- lib/handlebars/engine.rb,
lib/handlebars/engine/version.rb,
lib/handlebars/engine/function.rb
Overview
The Handlebars engine.
This API follows the JavaScript API as closely as possible: handlebarsjs.com/api-reference/.
Defined Under Namespace
Classes: Function
Constant Summary collapse
- Error =
MiniRacer::RuntimeError
- VERSION =
"0.3.3"
Instance Method Summary collapse
-
#compile(*args) ⇒ Proc
Compiles a template so it can be executed immediately.
-
#initialize(lazy: false, path: nil) ⇒ Engine
constructor
Creates a new instance.
-
#precompile(*args) ⇒ String
Precompiles a given template so it can be executed without compilation.
-
#register_helper(name = nil, function = nil, **helpers) {|context, arguments, options| ... } ⇒ Object
Registers helpers accessible by any template in the environment.
-
#register_helper_missing(type = :basic) {|arguments, options| ... } ⇒ Object
Registers the hook called when a mustache or a block-statement is missing.
-
#register_partial(name = nil, partial = nil, **partials) ⇒ Object
Registers partials accessible by any template in the environment.
-
#register_partial_missing {|name| ... } ⇒ Object
Registers the hook called when a partial is missing.
-
#template(*args) ⇒ Proc
Sets up a template that was precompiled with ‘precompile`.
-
#unregister_helper(name) ⇒ Object
Unregisters a previously registered helper.
-
#unregister_helper_missing(type = :basic) ⇒ Object
Unregisters the previously registered hook.
-
#unregister_partial(name) ⇒ Object
Unregisters a previously registered partial.
-
#unregister_partial_missing ⇒ Object
Unregisters the previously registered hook.
-
#version ⇒ String
Returns the version of Handlebars.
Constructor Details
#initialize(lazy: false, path: nil) ⇒ Engine
Creates a new instance.
24 25 26 27 |
# File 'lib/handlebars/engine.rb', line 24 def initialize(lazy: false, path: nil) @path = path init! unless lazy end |
Instance Method Details
#compile(*args) ⇒ Proc
Compiles a template so it can be executed immediately.
39 40 41 |
# File 'lib/handlebars/engine.rb', line 39 def compile(*args) call(__method__, args, assign: true) end |
#precompile(*args) ⇒ String
Precompiles a given template so it can be executed without compilation.
49 50 51 |
# File 'lib/handlebars/engine.rb', line 49 def precompile(*args) call(__method__, args) end |
#register_helper(name = nil, function = nil, **helpers) {|context, arguments, options| ... } ⇒ Object
Registers helpers accessible by any template in the environment.
The function can be either a proc or a string:
-
When the function is a proc, it can be either passed in as a normal parameter or as a block.
-
When the function is a string, it is interpreted as a JavaScript function.
81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/handlebars/engine.rb', line 81 def register_helper(name = nil, function = nil, **helpers, &block) helpers[name] = block || function if name helpers.each do |n, f| case f when Proc attach(n, &f) evaluate("registerHelper('#{n}', #{n})") when String, Symbol evaluate("Handlebars.registerHelper('#{n}', #{f})") end end end |
#register_helper_missing(type = :basic) {|arguments, options| ... } ⇒ Object
Registers the hook called when a mustache or a block-statement is missing.
130 131 132 133 |
# File 'lib/handlebars/engine.rb', line 130 def register_helper_missing(type = :basic, &block) name = helper_missing_name(type) register_helper(name, &block) end |
#register_partial(name = nil, partial = nil, **partials) ⇒ Object
Registers partials accessible by any template in the environment.
107 108 109 110 |
# File 'lib/handlebars/engine.rb', line 107 def register_partial(name = nil, partial = nil, **partials) partials[name] = partial if name call(:registerPartial, [partials]) end |
#register_partial_missing {|name| ... } ⇒ Object
Registers the hook called when a partial is missing.
Note: This is not a part of the offical Handlebars API. It is provided for convenience.
150 151 152 |
# File 'lib/handlebars/engine.rb', line 150 def register_partial_missing(&block) attach(:partialMissing, &block) end |
#template(*args) ⇒ Proc
Sets up a template that was precompiled with ‘precompile`.
59 60 61 |
# File 'lib/handlebars/engine.rb', line 59 def template(*args) call(__method__, args, assign: true) end |
#unregister_helper(name) ⇒ Object
Unregisters a previously registered helper.
98 99 100 |
# File 'lib/handlebars/engine.rb', line 98 def unregister_helper(name) call(:unregisterHelper, [name]) end |
#unregister_helper_missing(type = :basic) ⇒ Object
Unregisters the previously registered hook.
139 140 141 142 |
# File 'lib/handlebars/engine.rb', line 139 def unregister_helper_missing(type = :basic) name = helper_missing_name(type) unregister_helper(name) end |
#unregister_partial(name) ⇒ Object
Unregisters a previously registered partial.
116 117 118 |
# File 'lib/handlebars/engine.rb', line 116 def unregister_partial(name) call(:unregisterPartial, [name]) end |
#unregister_partial_missing ⇒ Object
Unregisters the previously registered hook.
155 156 157 |
# File 'lib/handlebars/engine.rb', line 155 def unregister_partial_missing evaluate("delete partialMissing") end |
#version ⇒ String
Returns the version of Handlebars.
166 167 168 |
# File 'lib/handlebars/engine.rb', line 166 def version evaluate("VERSION") end |