class Luigi::Cache
Minimal lazy-loading template cache.
Group a set of templates together and only parse them on an as-needed basis.
Public Class Methods
new(strings, filters = FILTERS)
click to toggle source
Create a new template cache with the given templates.
Parameters:
-
strings
: Map of template names to template strings. -
filters
: Hash of filters. Defaults to Luigi::FILTERS if unspecified.
Example:
# create template cache cache = Luigi::Cache.new({ hi: 'hi %{name}!' }) # run template from cache puts cache.run(:hi, { name: 'Paul' }) # prints "hi paul!"
# File lib/luigi-template.rb, line 843 def initialize(strings, filters = FILTERS) # work with frozen copy of strings hash strings = strings.freeze @templates = Hash.new do |h, k| # always deal with symbols k = k.intern # make sure template exists raise UnknownTemplateError.new(k) unless strings.key?(k) # create template h[k] = Template.new(strings[k], filters) end end
Public Instance Methods
[](key)
click to toggle source
Get given template, or raise an UnknownTemplateError if the given template does not exist.
Example:
# create template cache cache = Luigi::Cache.new({ hi: 'hi %{name}!' }) # get template from cache tmpl = cache[:hi] # run template, print result puts tmpl.run(:hi, { name: 'Paul' }) # prints "hi Paul"
# File lib/luigi-template.rb, line 880 def [](key) @templates[key] end
run(key, args)
click to toggle source
Run specified template from cache with the given templates.
Raises an UnknownTemplateError if the given template key does not exist.
Parameters:
-
key
: Template key. -
args
: Argument key/value map.
Example:
# create template cache cache = Luigi::Cache.new({ hi: 'hi %{name}!' }) # run template from cache puts cache.run(:hi, { name: 'Paul' }) # prints "hi paul!"
# File lib/luigi-template.rb, line 909 def run(key, args) # run template with args and return result @templates[key].run(args) end