Class: Mullet::HTML::TemplateLoader
- Inherits:
-
Object
- Object
- Mullet::HTML::TemplateLoader
- 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
-
#template_path ⇒ Object
Returns the value of attribute template_path.
Instance Method Summary collapse
-
#initialize(template_path) ⇒ TemplateLoader
constructor
Constructor.
-
#load(uri) ⇒ Object
Loads named template.
-
#on_missing(strategy) ⇒ TemplateLoader
Sets block to execute on attempt to render a variable that was not found.
-
#on_nil(strategy) ⇒ TemplateLoader
Sets block to execute on attempt to render a nil value.
Constructor Details
#initialize(template_path) ⇒ TemplateLoader
Constructor
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_path ⇒ Object
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.
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.
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.
44 45 46 47 |
# File 'lib/mullet/html/template_loader.rb', line 44 def on_nil(strategy) @on_nil = strategy return self end |