Class: Tilt::HandlebarsTemplate

Inherits:
Template
  • Object
show all
Defined in:
lib/tilt/handlebars.rb

Overview

A Tilt interface for the official JavaScript implementation of the Handlebars.js template engine.

Constant Summary collapse

EXTENSIONS =
["handlebars", "hbs"].freeze

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object (private)



86
87
88
# File 'lib/tilt/handlebars.rb', line 86

def method_missing(name, *args, &block)
  @engine.send(name, *args, &block)
end

Instance Method Details

#allows_script?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/tilt/handlebars.rb', line 58

def allows_script?
  false
end

#evaluate(scope, locals, &block) ⇒ Object

rubocop:disable Metrics/AbcSize



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/tilt/handlebars.rb', line 34

def evaluate(scope, locals, &block)
  # Based on LiquidTemplate
  locals = locals.transform_keys(&:to_s)
  if scope.respond_to?(:to_h)
    scope = scope.to_h.transform_keys(&:to_s)
    locals = scope.merge(locals)
  else
    scope.instance_variables.each do |var|
      key = var.to_s.delete("@")
      locals[key] = scope.instance_variable_get(var) unless locals.key? key
    end
  end

  locals["yield"] = block.nil? ? "" : yield
  locals["content"] = locals["yield"]

  @template.call(locals)
end

#initialize_engineObject

Loads the template engine, if necessary.

This method is needed in Tilt 1 but was removed in Tilt 2. It is provided here for backwards compatibility.



25
# File 'lib/tilt/handlebars.rb', line 25

def initialize_engine; end

#partial_missing(*args, **opts, &block) ⇒ Object

rubocop:enable Metrics/AbcSize



54
55
56
# File 'lib/tilt/handlebars.rb', line 54

def partial_missing(*args, **opts, &block)
  @engine.register_partial_missing(*args, **opts, &block)
end

#prepareObject



27
28
29
30
31
# File 'lib/tilt/handlebars.rb', line 27

def prepare
  @engine = ::Handlebars::Engine.new(**options.slice(:lazy, :path))
  @engine.register_partial_missing { |name| load_partial(name) }
  @template = @engine.compile(data)
end