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 local fs = require"luci.fs"
28 local sys = require "luci.sys"
29 local util = require "luci.util"
30 local table = require "table"
31 local string = require "string"
32 local config = require "luci.config"
33 local coroutine = require "coroutine"
35 local tostring, pairs, loadstring = tostring, pairs, loadstring
36 local setmetatable, loadfile = setmetatable, loadfile
37 local getfenv, setfenv, rawget = getfenv, setfenv, rawget
38 local assert, type, error = assert, type, error
40 --- LuCI template library.
41 module "luci.template"
43 config.template = config.template or {}
45 viewdir = config.template.viewdir or util.libpath() .. "/view"
46 compiledir = config.template.compiledir or util.libpath() .. "/view"
50 -- memory: Always compile, do not save compiled files, ignore precompiled
51 -- file: Compile on demand, save compiled files, update precompiled
52 compiler_mode = config.template.compiler_mode or "memory"
55 -- Define the namespace for template modules
56 context = util.threadlocal()
58 --- Manually compile a given template into an executable Lua function
59 -- @param template LuCI template
60 -- @return Lua template function
61 function compile(template)
64 -- Search all <% %> expressions
65 local function expr_add(ws1, skip1, command, skip2, ws2)
66 expr[#expr+1] = command
67 return ( #skip1 > 0 and "" or ws1 ) ..
68 "<%" .. tostring(#expr) .. "%>" ..
69 ( #skip2 > 0 and "" or ws2 )
72 -- Save all expressiosn to table "expr"
73 template = template:gsub("(%s*)<%%(%-?)(.-)(%-?)%%>(%s*)", expr_add)
75 local function sanitize(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 (optional)
125 function render(name, scope)
126 return Template(name):render(scope or getfenv(2))
131 Template = util.class()
133 -- Shared template cache to store templates in to avoid unnecessary reloading
134 Template.cache = setmetatable({}, {__mode = "v"})
137 -- Constructor - Reads and compiles the template on-demand
138 function Template.__init__(self, name)
139 local function _encode_filename(str)
141 local function __chrenc( chr )
142 return "%%%02x" % string.byte( chr )
145 if type(str) == "string" then
147 "([^a-zA-Z0-9$_%-%.%+!*'(),])",
155 self.template = self.cache[name]
158 -- Create a new namespace for this template
159 self.viewns = context.viewns
161 -- If we have a cached template, skip compiling and loading
162 if self.template then
166 -- Enforce cache security
167 local cdir = compiledir .. "/" .. sys.process.info("uid")
170 local sourcefile = viewdir .. "/" .. name
171 local compiledfile = cdir .. "/" .. _encode_filename(name) .. ".lua"
174 if compiler_mode == "file" then
175 local tplmt = fs.mtime(sourcefile) or fs.mtime(sourcefile .. ".htm")
176 local commt = fs.mtime(compiledfile)
178 if not fs.mtime(cdir) then
180 fs.chmod(fs.dirname(cdir), "a+rxw")
183 assert(tplmt or commt, "No such template: " .. name)
185 -- Build if there is no compiled file or if compiled file is outdated
186 if not commt or (commt and tplmt and commt < tplmt) then
188 source, err = fs.readfile(sourcefile) or fs.readfile(sourcefile .. ".htm")
191 local compiled, err = compile(source)
193 fs.writefile(compiledfile, util.get_bytecode(compiled))
194 fs.chmod(compiledfile, "a-rwx,u+rw")
195 self.template = compiled
199 sys.process.info("uid") == fs.stat(compiledfile, "uid")
200 and fs.stat(compiledfile, "mode") == "rw-------",
201 "Fatal: Cachefile is not sane!"
203 self.template, err = loadfile(compiledfile)
206 elseif compiler_mode == "memory" then
208 source, err = fs.readfile(sourcefile) or fs.readfile(sourcefile .. ".htm")
210 self.template, err = compile(source)
215 -- If we have no valid template throw error, otherwise cache the template
216 if not self.template then
219 self.cache[name] = self.template
224 -- Renders a template
225 function Template.render(self, scope)
226 scope = scope or getfenv(2)
228 -- Put our predefined objects in the scope of the template
229 setfenv(self.template, setmetatable({}, {__index =
231 return rawget(tbl, key) or self.viewns[key] or scope[key]
234 -- Now finally render the thing
235 local stat, err = util.copcall(self.template)
237 error("Error in template %s: %s" % {self.name, err})