Class: Mullet::HTML::TemplateLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/mullet/html/template_loader.rb

Overview

Loads templates from files, and caches them for fast retrieval of already loaded templates.

By default, templates render an empty string when a variable is not found or its value is nil. Call the ‘on_missing` and `on_nil` methods to configure how templates loaded by this loader should handle missing and nil values respectively.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template_path) ⇒ TemplateLoader

Constructor

Parameters:

  • template_path (String)

    name of directory to load templates from



21
22
23
24
25
26
27
# File 'lib/mullet/html/template_loader.rb', line 21

def initialize(template_path)
  @template_path = template_path
  @template_cache = Hash.new()
  @parser = TemplateParser.new(self)
  @on_missing = Template::RETURN_EMPTY_STRING
  @on_nil = Template::RETURN_EMPTY_STRING
end

Instance Attribute Details

#template_pathObject

Returns the value of attribute template_path.



15
16
17
# File 'lib/mullet/html/template_loader.rb', line 15

def template_path
  @template_path
end

Instance Method Details

#load(uri) ⇒ Object

Loads named template.

Parameters:

  • uri (String)

    file name optionally followed by ‘#`id



53
54
55
56
57
58
59
60
61
62
# File 'lib/mullet/html/template_loader.rb', line 53

def load(uri)
  id = nil
  hash_index = uri.index('#')
  if hash_index
    id = uri[(hash_index + 1)..-1]
    uri = uri[0...hash_index]
  end

  return load_file(uri, id)
end

#on_missing(strategy) ⇒ TemplateLoader

Sets block to execute on attempt to render a variable that was not found.

Parameters:

  • strategy (Proc)

    The value returned from block will be rendered.

Returns:



34
35
36
37
# File 'lib/mullet/html/template_loader.rb', line 34

def on_missing(strategy)
  @on_missing = strategy
  return self
end

#on_nil(strategy) ⇒ TemplateLoader

Sets block to execute on attempt to render a nil value.

Parameters:

  • strategy (Proc)

    The value returned from block will be rendered.

Returns:



44
45
46
47
# File 'lib/mullet/html/template_loader.rb', line 44

def on_nil(strategy)
  @on_nil = strategy
  return self
end