Class: Handlebarsjs::Handlebars
- Inherits:
-
Javascript
- Object
- Javascript
- Handlebarsjs::Handlebars
- Defined in:
- lib/handlebarsjs/handlebars.rb
Overview
API for interacting with Handlebars.js while providing native Ruby helpers
Instance Attribute Summary
Attributes inherited from Javascript
Class Method Summary collapse
- .block_helper(name, parameter_names) ⇒ Object
- .block_helper_safe(name, parameter_names) ⇒ Object
-
.function_helper(name) ⇒ Object
In handlebars, this is a simple function expression.
-
.function_helper_safe(name, parameter_names) ⇒ Object
In handlebars, this is also a simple function expression that returns a SafeString when HTML is desired.
- .register_helper_script(name, parameter_names = [], safe: false, block: false) ⇒ Object
Instance Method Summary collapse
-
#initialize ⇒ Handlebars
constructor
A new instance of Handlebars.
- #process_template(template, options = {}) ⇒ Object
Methods inherited from Javascript
Constructor Details
#initialize ⇒ Handlebars
Returns a new instance of Handlebars.
6 7 8 9 10 11 |
# File 'lib/handlebarsjs/handlebars.rb', line 6 def initialize super add_libraries add_configured_helpers end |
Class Method Details
.block_helper(name, parameter_names) ⇒ Object
46 47 48 49 50 51 52 53 54 55 |
# File 'lib/handlebarsjs/handlebars.rb', line 46 def block_helper(name, parameter_names) parameters = (parameter_names + ['opts']).join(', ') ruby_parameters = parameters.length.positive? ? ", #{parameters}" : '' <<-JAVASCRIPT Handlebars.registerHelper('#{name}', function (#{parameters}) { return ruby_#{name}(opts.fn(this)#{ruby_parameters}); }) JAVASCRIPT end |
.block_helper_safe(name, parameter_names) ⇒ Object
57 58 59 60 61 62 63 64 65 66 |
# File 'lib/handlebarsjs/handlebars.rb', line 57 def block_helper_safe(name, parameter_names) parameters = (parameter_names + ['opts']).join(', ') ruby_parameters = parameters.length.positive? ? ", #{parameters}" : '' <<-JAVASCRIPT Handlebars.registerHelper('#{name}', function (#{parameters}) { return new Handlebars.SafeString(ruby_#{name}(opts.fn(this)#{ruby_parameters})); }) JAVASCRIPT end |
.function_helper(name) ⇒ Object
In handlebars, this is a simple function expression
28 29 30 31 32 33 |
# File 'lib/handlebarsjs/handlebars.rb', line 28 def function_helper(name) # Not sure why I am not using paramater names, but currently it works for the existing use cases <<-JAVASCRIPT Handlebars.registerHelper('#{name}', ruby_#{name}) JAVASCRIPT end |
.function_helper_safe(name, parameter_names) ⇒ Object
In handlebars, this is also a simple function expression that returns a SafeString when HTML is desired
36 37 38 39 40 41 42 43 44 |
# File 'lib/handlebarsjs/handlebars.rb', line 36 def function_helper_safe(name, parameter_names) parameters = (parameter_names + ['_opts']).join(', ') <<-JAVASCRIPT Handlebars.registerHelper('#{name}', function (#{parameters}) { return new Handlebars.SafeString(ruby_#{name}(#{parameters})); }) JAVASCRIPT end |
.register_helper_script(name, parameter_names = [], safe: false, block: false) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/handlebarsjs/handlebars.rb', line 14 def register_helper_script(name, parameter_names = [], safe: false, block: false) if safe && block block_helper_safe(name, parameter_names) elsif safe # register_safe_string_helper_script(name, parameter_names) function_helper_safe(name, parameter_names) elsif block block_helper(name, parameter_names) else function_helper(name) end end |
Instance Method Details
#process_template(template, options = {}) ⇒ Object
69 70 71 72 |
# File 'lib/handlebarsjs/handlebars.rb', line 69 def process_template(template, = {}) # TODO: process template function may be improved with some type of caching context.call('process_template', template, ) end |