Class: Handlebars::Engine
- Inherits:
-
Object
- Object
- Handlebars::Engine
- Defined in:
- lib/handlebars/engine.rb,
lib/handlebars/engine/version.rb
Overview
The Handlebars engine.
This API follows the JavaScript API as closely as possible: https://handlebarsjs.com/api-reference/.
Constant Summary collapse
- Error =
MiniRacer::RuntimeError
- VERSION =
"0.3.1"
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.
23 24 25 26 |
# File 'lib/handlebars/engine.rb', line 23 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.
38 39 40 |
# File 'lib/handlebars/engine.rb', line 38 def compile(*args) call(__method__, args, assign: true) end |
#precompile(*args) ⇒ String
Precompiles a given template so it can be executed without compilation.
48 49 50 |
# File 'lib/handlebars/engine.rb', line 48 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.
80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/handlebars/engine.rb', line 80 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.
129 130 131 132 |
# File 'lib/handlebars/engine.rb', line 129 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.
106 107 108 109 |
# File 'lib/handlebars/engine.rb', line 106 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.
149 150 151 |
# File 'lib/handlebars/engine.rb', line 149 def register_partial_missing(&block) attach(:partialMissing, &block) end |
#template(*args) ⇒ Proc
Sets up a template that was precompiled with precompile.
58 59 60 |
# File 'lib/handlebars/engine.rb', line 58 def template(*args) call(__method__, args, assign: true) end |
#unregister_helper(name) ⇒ Object
Unregisters a previously registered helper.
97 98 99 |
# File 'lib/handlebars/engine.rb', line 97 def unregister_helper(name) call(:unregisterHelper, [name]) end |
#unregister_helper_missing(type = :basic) ⇒ Object
Unregisters the previously registered hook.
138 139 140 141 |
# File 'lib/handlebars/engine.rb', line 138 def unregister_helper_missing(type = :basic) name = helper_missing_name(type) unregister_helper(name) end |
#unregister_partial(name) ⇒ Object
Unregisters a previously registered partial.
115 116 117 |
# File 'lib/handlebars/engine.rb', line 115 def unregister_partial(name) call(:unregisterPartial, [name]) end |
#unregister_partial_missing ⇒ Object
Unregisters the previously registered hook.
154 155 156 |
# File 'lib/handlebars/engine.rb', line 154 def unregister_partial_missing evaluate("delete partialMissing") end |
#version ⇒ String
Returns the version of Handlebars.
165 166 167 |
# File 'lib/handlebars/engine.rb', line 165 def version evaluate("VERSION") end |