Class: Handlebarsjs::Handlebars

Inherits:
Javascript show all
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

#handlebars_snapshot

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Javascript

#attach, #call, #eval

Constructor Details

#initializeHandlebars

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, options = {})
  # TODO: process template function may be improved with some type of caching
  context.call('process_template', template, options)
end