Class: Handlebars::Engine

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

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.



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.

Parameters:

  • template (String)

    the template string to compile

  • options (Hash)

    the options

Returns:

  • (Proc)

    the template function to call

See Also:



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.

Parameters:

  • template (String)

    the template string to precompiled

  • options (Hash)

    the options

Returns:

  • (String)

    the precompiled template spec

See Also:



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.

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:



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.

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:



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.

Parameters:

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

    the name of the partial

  • partial (String) (defaults to: nil)

    the partial template

See Also:



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.

Yield Parameters:

  • name (String)

    the name of the undefined partial



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`.

Parameters:

  • spec (String)

    the precompiled template spec

Returns:

  • (Proc)

    the template function to call

See Also:



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.

Parameters:

  • name (String, Symbol)

    the name of the helper

See Also:



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.

Parameters:

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

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

See Also:



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.

Parameters:

  • name (String, Symbol)

    the name of the partial

See Also:



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

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

#unregister_partial_missingObject

Unregisters the previously registered hook.



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

def unregister_partial_missing
  evaluate("delete partialMissing")
end

#versionString

Returns the version of Handlebars.

Returns:

  • (String)

    the Handlebars version.



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

def version
  evaluate("VERSION")
end