summary |
shortlog |
log |
commit | commitdiff |
tree
raw |
patch |
inline | side by side (from parent 1:
35eaa9f)
Signed-off-by: Matthias Schiffer <mschiffer@universe-factory.net>
return Template(name):render(scope or getfenv(2))
end
return Template(name):render(scope or getfenv(2))
end
+--- Render a template from a string.
+-- @param template Template string
+-- @param scope Scope to assign to template (optional)
+function render_string(template, scope)
+ return Template(nil, template):render(scope or getfenv(2))
+end
+
-- Template class
Template = util.class()
-- Template class
Template = util.class()
-- Constructor - Reads and compiles the template on-demand
-- Constructor - Reads and compiles the template on-demand
-function Template.__init__(self, name)
+function Template.__init__(self, name, template)
+ if name then
+ self.template = self.cache[name]
+ self.name = name
+ else
+ self.name = "[string]"
+ end
- self.template = self.cache[name]
- self.name = name
-
-- Create a new namespace for this template
self.viewns = context.viewns
-- Create a new namespace for this template
self.viewns = context.viewns
-- Compile template
local err
-- Compile template
local err
- local sourcefile = viewdir .. "/" .. name .. ".htm"
- self.template, _, err = tparser.parse(sourcefile)
+ if name then
+ sourcefile = viewdir .. "/" .. name .. ".htm"
+ self.template, _, err = tparser.parse(sourcefile)
+ else
+ sourcefile = "[string]"
+ self.template, _, err = tparser.parse_string(template)
+ end
-- If we have no valid template throw error, otherwise cache the template
if not self.template then
error("Failed to load template '" .. name .. "'.\n" ..
"Error while parsing template '" .. sourcefile .. "':\n" ..
(err or "Unknown syntax error"))
-- If we have no valid template throw error, otherwise cache the template
if not self.template then
error("Failed to load template '" .. name .. "'.\n" ..
"Error while parsing template '" .. sourcefile .. "':\n" ..
(err or "Unknown syntax error"))
self.cache[name] = self.template
end
end
self.cache[name] = self.template
end
end