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.
27 --- LuCI template library.
28 module("luci.template", package.seeall)
30 require("luci.config")
36 luci.config.template = luci.config.template or {}
38 viewdir = luci.config.template.viewdir or luci.util.libpath() .. "/view"
39 compiledir = luci.config.template.compiledir or luci.util.libpath() .. "/view"
43 -- none: Never compile, only use precompiled data from files
44 -- memory: Always compile, do not save compiled files, ignore precompiled
45 -- file: Compile on demand, save compiled files, update precompiled
46 compiler_mode = luci.config.template.compiler_mode or "memory"
49 -- Define the namespace for template modules
50 context = luci.util.threadlocal()
53 include = function(name) Template(name):render(getfenv(2)) end,
56 --- Manually compile a given template into an executable Lua function
57 -- @param template LuCI template
58 -- @return Lua template function
59 function compile(template)
60 -- Search all <% %> expressions
61 local function expr_add(ws1, skip1, command, skip2, ws2)
62 table.insert(expr, command)
63 return ( #skip1 > 0 and "" or ws1 ) ..
64 "<%" .. tostring(#expr) .. "%>" ..
65 ( #skip2 > 0 and "" or ws2 )
68 -- As "expr" should be local, we have to assign it to the "expr_add" scope
70 luci.util.extfenv(expr_add, "expr", expr)
72 -- Save all expressiosn to table "expr"
73 template = template:gsub("(%s*)<%%(%-?)(.-)(%-?)%%>(%s*)", expr_add)
75 local function sanitize(s)
76 s = string.format("%q", s)
80 -- Escape and sanitize all the template (all non-expressions)
81 template = sanitize(template)
83 -- Template module header/footer declaration
84 local header = 'write("'
87 template = header .. template .. footer
90 local r_include = '")\ninclude("%s")\nwrite("'
91 local r_i18n = '"..translate("%1","%2").."'
92 local r_i18n2 = '"..translate("%1", "").."'
93 local r_pexec = '"..(%s or "").."'
94 local r_exec = '")\n%s\nwrite("'
96 -- Parse the expressions
97 for k,v in pairs(expr) do
99 v = v:gsub("%%", "%%%%")
102 re = r_include:format(sanitize(string.sub(v, 2)))
105 re = sanitize(v):gsub(":(.-) (.*)", r_i18n)
107 re = sanitize(v):gsub(":(.+)", r_i18n2)
110 re = r_pexec:format(v:sub(2))
114 re = r_exec:format(v)
116 template = template:gsub("<%%"..tostring(k).."%%>", re)
119 return loadstring(template)
122 --- Render a certain template.
123 -- @param name Template name
124 -- @param scope Scope to assign to template
125 function render(name, scope, ...)
126 scope = scope or getfenv(2)
127 local s, t = luci.util.copcall(Template, name)
137 Template = luci.util.class()
139 -- Shared template cache to store templates in to avoid unnecessary reloading
141 setmetatable(Template.cache, {__mode = "v"})
144 -- Constructor - Reads and compiles the template on-demand
145 function Template.__init__(self, name)
146 self.template = self.cache[name]
148 -- Create a new namespace for this template
151 -- Copy over from general namespace
152 luci.util.update(self.viewns, viewns)
153 if context.viewns then
154 luci.util.update(self.viewns, context.viewns)
157 -- If we have a cached template, skip compiling and loading
158 if self.template then
162 -- Enforce cache security
163 local cdir = compiledir .. "/" .. luci.sys.process.info("uid")
166 local sourcefile = viewdir .. "/" .. name .. ".htm"
167 local compiledfile = cdir .. "/" .. luci.http.urlencode(name) .. ".lua"
170 if compiler_mode == "file" then
171 local tplmt = luci.fs.mtime(sourcefile)
172 local commt = luci.fs.mtime(compiledfile)
174 if not luci.fs.mtime(cdir) then
175 luci.fs.mkdir(cdir, true)
176 luci.fs.chmod(luci.fs.dirname(cdir), "a+rxw")
179 -- Build if there is no compiled file or if compiled file is outdated
180 if ((commt == nil) and not (tplmt == nil))
181 or (not (commt == nil) and not (tplmt == nil) and commt < tplmt) then
183 source, err = luci.fs.readfile(sourcefile)
186 local compiled, err = compile(source)
188 luci.fs.writefile(compiledfile, luci.util.get_bytecode(compiled))
189 self.template = compiled
192 self.template, err = loadfile(compiledfile)
195 elseif compiler_mode == "none" then
196 self.template, err = loadfile(self.compiledfile)
198 elseif compiler_mode == "memory" then
200 source, err = luci.fs.readfile(sourcefile)
202 self.template, err = compile(source)
207 -- If we have no valid template throw error, otherwise cache the template
208 if not self.template then
211 self.cache[name] = self.template
216 -- Renders a template
217 function Template.render(self, scope)
218 scope = scope or getfenv(2)
220 -- Save old environment
221 local oldfenv = getfenv(self.template)
223 -- Put our predefined objects in the scope of the template
224 luci.util.resfenv(self.template)
225 luci.util.updfenv(self.template, scope)
226 luci.util.updfenv(self.template, self.viewns)
228 -- Now finally render the thing
232 setfenv(self.template, oldfenv)