Class: Minibars::Template

Inherits:
Object
  • Object
show all
Defined in:
lib/minibars/template.rb

Overview

A compiled handlebars template.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context, js, content) ⇒ Template

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Template.



14
15
16
17
18
19
# File 'lib/minibars/template.rb', line 14

def initialize(context, js, content)
  @content = content
  @context = context
  @js      = js
  @name    = "Minibars_Template_#{hash_combine(@context.hash, @content.hash).abs}"
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



6
7
8
# File 'lib/minibars/template.rb', line 6

def content
  @content
end

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/minibars/template.rb', line 6

def name
  @name
end

Class Method Details

.compile(context, js, content) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



9
10
11
# File 'lib/minibars/template.rb', line 9

def self.compile(context, js, content)
  new(context, js, content).compile
end

Instance Method Details

#call(params = EMPTY_HASH) ⇒ String

Render the template with the given parameters.



35
36
37
# File 'lib/minibars/template.rb', line 35

def call(params = EMPTY_HASH)
  @js.eval("#{name}(#{JSON.generate process_params(params)})")
end

#compileObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Raises:



22
23
24
25
26
27
28
# File 'lib/minibars/template.rb', line 22

def compile
  raise Error, 'Template content should be a string' unless content.is_a?(String)

  @js.eval("#{name} = Handlebars.compile(#{content.to_json})")

  self
end