5 A template parser supporting includes, translations, Lua code blocks
6 and more. It can be used either as a compiler or as an interpreter.
11 Copyright 2008 Steven Barth <steven@midlink.org>
13 Licensed under the Apache License, Version 2.0 (the "License");
14 you may not use this file except in compliance with the License.
15 You may obtain a copy of the License at
17 http://www.apache.org/licenses/LICENSE-2.0
19 Unless required by applicable law or agreed to in writing, software
20 distributed under the License is distributed on an "AS IS" BASIS,
21 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 See the License for the specific language governing permissions and
23 limitations under the License.
26 module("luci.template", package.seeall)
28 require("luci.config")
33 luci.config.template = luci.config.template or {}
35 viewdir = luci.config.template.viewdir or luci.sys.libpath() .. "/view"
36 compiledir = luci.config.template.compiledir or luci.sys.libpath() .. "/view"
40 -- none: Never compile, only use precompiled data from files
41 -- memory: Always compile, do not save compiled files, ignore precompiled
42 -- file: Compile on demand, save compiled files, update precompiled
43 compiler_mode = luci.config.template.compiler_mode or "memory"
46 -- Define the namespace for template modules
49 include = function(name) Template(name):render(getfenv(2)) end,
52 -- Compiles a given template into an executable Lua module
53 function compile(template)
54 -- Search all <% %> expressions (remember: Lua table indexes begin with #1)
55 local function expr_add(command)
56 table.insert(expr, command)
57 return "<%" .. tostring(#expr) .. "%>"
60 -- As "expr" should be local, we have to assign it to the "expr_add" scope
62 luci.util.extfenv(expr_add, "expr", expr)
64 -- Save all expressiosn to table "expr"
65 template = template:gsub("<%%(.-)%%>", expr_add)
67 local function sanitize(s)
68 s = luci.util.escape(s)
69 s = luci.util.escape(s, "'")
70 s = luci.util.escape(s, "\n")
74 -- Escape and sanitize all the template (all non-expressions)
75 template = sanitize(template)
77 -- Template module header/footer declaration
78 local header = "write('"
81 template = header .. template .. footer
84 local r_include = "')\ninclude('%s')\nwrite('"
85 local r_i18n = "'..translate('%1','%2')..'"
86 local r_pexec = "'..(%s or '')..'"
87 local r_exec = "')\n%s\nwrite('"
89 -- Parse the expressions
90 for k,v in pairs(expr) do
94 re = r_include:format(sanitize(string.sub(v, 2)))
96 re = sanitize(v):gsub(":(.-) (.+)", r_i18n)
98 re = r_pexec:format(v:sub(2))
100 re = r_exec:format(v)
102 template = template:gsub("<%%"..tostring(k).."%%>", re)
105 return loadstring(template)
108 -- Oldstyle render shortcut
109 function render(name, scope, ...)
110 scope = scope or getfenv(2)
111 local s, t = pcall(Template, name)
121 Template = luci.util.class()
123 -- Shared template cache to store templates in to avoid unnecessary reloading
127 -- Constructor - Reads and compiles the template on-demand
128 function Template.__init__(self, name)
129 if self.cache[name] then
130 self.template = self.cache[name]
135 -- Create a new namespace for this template
138 -- Copy over from general namespace
139 for k, v in pairs(viewns) do
143 -- If we have a cached template, skip compiling and loading
144 if self.template then
148 -- Enforce cache security
149 local cdir = compiledir .. "/" .. luci.sys.process.info("uid")
152 local sourcefile = viewdir .. "/" .. name .. ".htm"
153 local compiledfile = cdir .. "/" .. luci.http.urlencode(name) .. ".lua"
156 if compiler_mode == "file" then
157 local tplmt = luci.fs.mtime(sourcefile)
158 local commt = luci.fs.mtime(compiledfile)
160 if not luci.fs.mtime(cdir) then
161 luci.fs.mkdir(cdir, true)
162 luci.fs.chmod(luci.fs.dirname(cdir), "a+rxw")
165 -- Build if there is no compiled file or if compiled file is outdated
166 if ((commt == nil) and not (tplmt == nil))
167 or (not (commt == nil) and not (tplmt == nil) and commt < tplmt) then
169 source, err = luci.fs.readfile(sourcefile)
172 local compiled, err = compile(source)
174 luci.fs.writefile(compiledfile, luci.util.dump(compiled))
175 self.template = compiled
178 self.template, err = loadfile(compiledfile)
181 elseif compiler_mode == "none" then
182 self.template, err = loadfile(self.compiledfile)
184 elseif compiler_mode == "memory" then
186 source, err = luci.fs.readfile(sourcefile)
188 self.template, err = compile(source)
193 -- If we have no valid template throw error, otherwise cache the template
194 if not self.template then
197 self.cache[name] = self.template
202 -- Renders a template
203 function Template.render(self, scope)
204 scope = scope or getfenv(2)
206 -- Save old environment
207 local oldfenv = getfenv(self.template)
209 -- Put our predefined objects in the scope of the template
210 luci.util.resfenv(self.template)
211 luci.util.updfenv(self.template, scope)
212 luci.util.updfenv(self.template, self.viewns)
214 -- Now finally render the thing
218 setfenv(self.template, oldfenv)