require("luci.fs")
require("luci.http")
-viewdir = luci.sys.libpath() .. "/view/"
+luci.config.template = luci.config.template or {}
+
+viewdir = luci.config.template.viewdir or luci.sys.libpath() .. "/view"
+compiledir = luci.config.template.compiledir or luci.sys.libpath() .. "/view"
+
+-- Enforce cache security
+compiledir = compiledir .. "/" .. luci.sys.process.info("uid")
-- Compile modes:
-- none: Never compile, only use precompiled data from files
-- memory: Always compile, do not save compiled files, ignore precompiled
-- file: Compile on demand, save compiled files, update precompiled
-compiler_mode = "memory"
-
-
--- This applies to compiler modes "always" and "smart"
---
--- Produce compiled lua code rather than lua sourcecode
--- WARNING: Increases template size heavily!!!
--- This produces the same bytecode as luac but does not have a strip option
-compiler_enable_bytecode = false
+compiler_mode = luci.config.template.compiler_mode or "memory"
-- Define the namespace for template modules
template = template:gsub("<%%"..tostring(k).."%%>", re)
end
- if compiler_enable_bytecode then
- tf = loadstring(template)
- template = string.dump(tf)
- end
-
- return template
+ return loadstring(template)
end
-- Oldstyle render shortcut
end
-- Compile and build
- local sourcefile = viewdir .. name .. ".htm"
- local compiledfile = viewdir .. name .. ".lua"
+ local sourcefile = viewdir .. "/" .. name .. ".htm"
+ local compiledfile = compiledir .. "/" .. luci.http.urlencode(name) .. ".lua"
local err
if compiler_mode == "file" then
local tplmt = luci.fs.mtime(sourcefile)
local commt = luci.fs.mtime(compiledfile)
+
+ if not luci.fs.mtime(compiledir) then
+ luci.fs.mkdir(compiledir, true)
+ luci.fs.chmod(luci.fs.dirname(compiledir), "a+rxw")
+ end
-- Build if there is no compiled file or if compiled file is outdated
if ((commt == nil) and not (tplmt == nil))
source, err = luci.fs.readfile(sourcefile)
if source then
- local compiled = compile(source)
- luci.fs.writefile(compiledfile, compiled)
- self.template, err = loadstring(compiled)
+ local compiled, err = compile(source)
+
+ luci.fs.writefile(compiledfile, luci.util.dump(compiled))
+ self.template = compiled
end
else
self.template, err = loadfile(compiledfile)
local source
source, err = luci.fs.readfile(sourcefile)
if source then
- self.template, err = loadstring(compile(source))
+ self.template, err = compile(source)
end
end