Class: Tilt::HandlebarsTemplate

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

Overview

Handlebars.rb template implementation. See: github.com/cowboyd/handlebars.rb and handlebarsjs.com

Handlebars is a logic-less template rendered with JavaScript. Handlebars.rb is a Ruby wrapper around Handlebars, that allows Handlebars templates to be rendered server side.

Instance Method Summary collapse

Instance Method Details

#allows_script?Boolean

Returns:

  • (Boolean)


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

def allows_script?
  false
end

#evaluate(scope, locals = {}, &block) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/tilt/handlebars.rb', line 28

def evaluate(scope, locals = {}, &block)
  # Based on LiquidTemplate
  locals = locals.inject({}){ |h,(k,v)| h[k.to_s] = v ; h }
  if scope.respond_to?(:to_h)
    scope  = scope.to_h.inject({}){ |h,(k,v)| h[k.to_s] = v ; h }
    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.has_key? key
    end
  end

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

  @template.call(locals);
end

#initialize_engineObject



16
17
18
19
# File 'lib/tilt/handlebars.rb', line 16

def initialize_engine
  return if defined? ::Handlebars
  require_template_library 'handlebars'
end

#partial_missing(&fn) ⇒ Object



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

def partial_missing(&fn)
  @context.partial_missing(&fn)
end

#prepareObject



21
22
23
24
25
# File 'lib/tilt/handlebars.rb', line 21

def prepare
  @context = ::Handlebars::Context.new
  @context.partial_missing { |partial_name| load_partial partial_name }
  @template = @context.compile(data)
end

#register_helper(name, &fn) ⇒ Object



47
48
49
# File 'lib/tilt/handlebars.rb', line 47

def register_helper(name, &fn)
  @context.register_helper(name, &fn)
end

#register_partial(*args) ⇒ Object



51
52
53
# File 'lib/tilt/handlebars.rb', line 51

def register_partial(*args)
  @context.register_partial(*args)
end