Class: Volt::Templates

Inherits:
Object show all
Defined in:
lib/volt/volt/templates.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTemplates

Returns a new instance of Templates.



8
9
10
# File 'lib/volt/volt/templates.rb', line 8

def initialize
  @templates = {}
end

Instance Attribute Details

#template_loader=(value) ⇒ Object (writeonly)

On the server, we can delay loading the views until they are actually requeted. This sets up an instance variable to call to load.



6
7
8
# File 'lib/volt/volt/templates.rb', line 6

def template_loader=(value)
  @template_loader = value
end

Instance Method Details

#[](key) ⇒ Object



12
13
14
# File 'lib/volt/volt/templates.rb', line 12

def [](key)
  templates[key]
end

#add_template(name, template, bindings) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/volt/volt/templates.rb', line 16

def add_template(name, template, bindings)
  # First template gets priority.  The backend will load templates in order so
  # that local templates come in before gems (so they can be overridden).
  #
  # TODO: Currently this means we will send templates to the client that will
  # not get used because they are being overridden.  Need to detect that and
  # not send them.
  unless @templates[name]
    @templates[name] = { 'html' => template, 'bindings' => bindings }
  end
end

#templatesObject

Load the templates on first use if a loader was specified



29
30
31
32
33
34
35
36
37
# File 'lib/volt/volt/templates.rb', line 29

def templates
  if @template_loader
    # Load the templates
    @template_loader.call
    @template_loader = nil
  end

  @templates
end