require("ffluci.i18n")
require("ffluci.model.uci")
-viewdir = ffluci.fs.dirname(ffluci.util.__file__()) .. "view/"
+viewdir = ffluci.config.path .. "/view/"
-- Compile modes:
-- Define the namespace for template modules
viewns = {
translate = ffluci.i18n.translate,
- config = ffluci.model.uci.get,
+ config = function(...) return ffluci.model.uci.get(...) or "" end,
controller = os.getenv("SCRIPT_NAME"),
- media = ffluci.config.mediaurlbase,
+ media = ffluci.config.main.mediaurlbase,
write = io.write,
include = function(name) Template(name):render(getfenv(2)) end,
}
elseif p == "~" then
re = sanitize(v):gsub("~(.-)%.(.-)%.(.+)", r_uci)
elseif p == "=" then
- re = r_pexec:format(string.sub(v, 2))
+ re = r_pexec:format(v:sub(2))
else
re = r_exec:format(v)
end
end
-- Oldstyle render shortcut
-function render(name, ...)
+function render(name, scope, ...)
+ scope = scope or getfenv(2)
local s, t = pcall(Template, name)
if not s then
- error("Unable to load template: " .. name)
+ error(t)
else
- t:render(...)
+ t:render(scope, ...)
end
end
-- Compile and build
local sourcefile = viewdir .. name .. ".htm"
- local compiledfile = viewdir .. name .. ".lua"
+ local compiledfile = viewdir .. name .. ".lua"
+ local err
if compiler_mode == "file" then
local tplmt = ffluci.fs.mtime(sourcefile)
-- Build if there is no compiled file or if compiled file is outdated
if ((commt == nil) and not (tplmt == nil))
or (not (commt == nil) and not (tplmt == nil) and commt < tplmt) then
- local compiled = compile(ffluci.fs.readfile(sourcefile))
- ffluci.fs.writefile(compiledfile, compiled)
- self.template = loadstring(compiled)
+ local source
+ source, err = ffluci.fs.readfile(sourcefile)
+
+ if source then
+ local compiled = compile(source)
+ ffluci.fs.writefile(compiledfile, compiled)
+ self.template, err = loadstring(compiled)
+ end
else
- self.template = loadfile(compiledfile)
+ self.template, err = loadfile(compiledfile)
end
elseif compiler_mode == "none" then
- self.template = loadfile(self.compiledfile)
+ self.template, err = loadfile(self.compiledfile)
elseif compiler_mode == "memory" then
- self.template = loadstring(compile(ffluci.fs.readfile(sourcefile)))
+ local source
+ source, err = ffluci.fs.readfile(sourcefile)
+ if source then
+ self.template, err = loadstring(compile(source))
+ end
- else
- error("Invalid compiler mode: " .. compiler_mode)
-
end
-- If we have no valid template throw error, otherwise cache the template
if not self.template then
- error("Unable to load template: " .. name)
+ error(err)
else
self.cache[name] = self.template
end
local oldfenv = getfenv(self.template)
-- Put our predefined objects in the scope of the template
+ ffluci.util.resfenv(self.template)
ffluci.util.updfenv(self.template, scope)
ffluci.util.updfenv(self.template, self.viewns)