| Class | ServerSide::Template |
| In: |
lib/serverside/template.rb
|
| Parent: | Object |
The Template module implements an Erubis template rendering system. Templates are cached and automatically reloaded if the file changes.
Renders a template by first validating it, and by invoking it with the supplied binding.
# File lib/serverside/template.rb, line 37
37: def self.render(name, binding)
38: if template = validate(name)
39: template.result(binding)
40: else
41: raise RuntimeError, 'Template not found.'
42: end
43: end
Caches a template for later use. The stamp parameter is used only when the content of a template file is stored.
# File lib/serverside/template.rb, line 15
15: def self.set(name, body, stamp = nil)
16: @@templates[name] = [stamp, Erubis::Eruby.new(body)]
17: end
Validates the referenced template by checking its stamp. If the name refers to a file, its stamp is checked against the cache stamp, and it is reloaded if necessary. The function returns an ERB instance or nil if the template is not found.
# File lib/serverside/template.rb, line 23
23: def self.validate(name)
24: t = @@templates[name]
25: return t[1] if t && t[0].nil?
26: if File.file?(name)
27: stamp = File.mtime(name)
28: t = set(name, IO.read(name), stamp) if (!t || (stamp != t[0]))
29: t[1]
30: else
31: @@templates[name] = nil
32: end
33: end