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.4"

Instance Method Summary collapse

Constructor Details

#initialize(lazy: false, logger: nil, 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
28
# File 'lib/handlebars/engine.rb', line 24

def initialize(lazy: false, logger: nil, path: nil)
  @logger = logger
  @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:



40
41
42
# File 'lib/handlebars/engine.rb', line 40

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:



50
51
52
# File 'lib/handlebars/engine.rb', line 50

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:



82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/handlebars/engine.rb', line 82

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("registerRbHelper('#{n}', #{n})")
    when String, Symbol
      evaluate("registerJsHelper('#{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:



131
132
133
134
# File 'lib/handlebars/engine.rb', line 131

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:



108
109
110
111
# File 'lib/handlebars/engine.rb', line 108

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



151
152
153
# File 'lib/handlebars/engine.rb', line 151

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:



60
61
62
# File 'lib/handlebars/engine.rb', line 60

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:



99
100
101
# File 'lib/handlebars/engine.rb', line 99

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:



140
141
142
143
# File 'lib/handlebars/engine.rb', line 140

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:



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

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

#unregister_partial_missingObject

Unregisters the previously registered hook.



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

def unregister_partial_missing
  evaluate("delete partialMissing")
end

#versionString

Returns the version of Handlebars.

Returns:

  • (String)

    the Handlebars version.



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

def version
  evaluate("VERSION")
end