Class: Handlebars::Engine

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

Constructor Details

#initialize(lazy: false, path: nil) ⇒ Engine

Creates a new instance.

Parameters:

  • lazy (true, false) (defaults to: false)

    immediately loads and initializes the JavaScript environment.

  • path (String, nil) (defaults to: nil)

    the path to the version of Handlebars to load. If nil, the contents of Handlebars::Source.bundled_path is loaded.



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.

Parameters:

  • template (String)

    the template string to compile

  • options (Hash)

    the options

Returns:

  • (Proc)

    the template function to call

See Also:



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.

Parameters:

  • template (String)

    the template string to precompiled

  • options (Hash)

    the options

Returns:

  • (String)

    the precompiled template spec

See Also:



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.

Parameters:

  • name (String, Symbol) (defaults to: nil)

    the name of the helper

  • function (Proc, String) (defaults to: nil)

    the helper function

Yield Parameters:

  • context (Hash)

    the current context

  • arguments (Object)

    the arguments (optional)

  • options (Hash)

    the options hash (optional)

See Also:



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.

Parameters:

  • type (Symbol) (defaults to: :basic)

    the type of hook to register (:basic or :block)

Yield Parameters:

  • arguments (Object)

    the arguments (optional)

  • options (Hash)

    the options hash (optional)

See Also:



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.

Parameters:

  • name (String, Symbol) (defaults to: nil)

    the name of the partial

  • partial (String) (defaults to: nil)

    the partial template

See Also:



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.

Yield Parameters:

  • name (String)

    the name of the undefined partial



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.

Parameters:

  • spec (String)

    the precompiled template spec

Returns:

  • (Proc)

    the template function to call

See Also:



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.

Parameters:

  • name (String, Symbol)

    the name of the helper

See Also:



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.

Parameters:

  • type (Symbol) (defaults to: :basic)

    the type of hook to register (:basic or :block)

See Also:



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.

Parameters:

  • name (String, Symbol)

    the name of the partial

See Also:



115
116
117
# File 'lib/handlebars/engine.rb', line 115

def unregister_partial(name)
  call(:unregisterPartial, [name])
end

#unregister_partial_missingObject

Unregisters the previously registered hook.



154
155
156
# File 'lib/handlebars/engine.rb', line 154

def unregister_partial_missing
  evaluate("delete partialMissing")
end

#versionString

Returns the version of Handlebars.

Returns:

  • (String)

    the Handlebars version.



165
166
167
# File 'lib/handlebars/engine.rb', line 165

def version
  evaluate("VERSION")
end