Merge pull request #278 from nmav/ocserv
[project/luci.git] / modules / base / luasrc / template.lua
1 --[[
2 LuCI - Template Parser
3
4 Description:
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.
7
8 FileId: $Id$
9
10 License:
11 Copyright 2008 Steven Barth <steven@midlink.org>
12
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
16
17 http://www.apache.org/licenses/LICENSE-2.0
18
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.
24
25 ]]--
26
27 local util = require "luci.util"
28 local config = require "luci.config"
29 local tparser = require "luci.template.parser"
30
31 local tostring, pairs, loadstring = tostring, pairs, loadstring
32 local setmetatable, loadfile = setmetatable, loadfile
33 local getfenv, setfenv, rawget = getfenv, setfenv, rawget
34 local assert, type, error = assert, type, error
35
36 --- LuCI template library.
37 module "luci.template"
38
39 config.template = config.template or {}
40 viewdir = config.template.viewdir or util.libpath() .. "/view"
41
42
43 -- Define the namespace for template modules
44 context = util.threadlocal()
45
46 --- Render a certain template.
47 -- @param name Template name
48 -- @param scope Scope to assign to template (optional)
49 function render(name, scope)
50 return Template(name):render(scope or getfenv(2))
51 end
52
53 --- Render a template from a string.
54 -- @param template Template string
55 -- @param scope Scope to assign to template (optional)
56 function render_string(template, scope)
57 return Template(nil, template):render(scope or getfenv(2))
58 end
59
60
61 -- Template class
62 Template = util.class()
63
64 -- Shared template cache to store templates in to avoid unnecessary reloading
65 Template.cache = setmetatable({}, {__mode = "v"})
66
67
68 -- Constructor - Reads and compiles the template on-demand
69 function Template.__init__(self, name, template)
70 if name then
71 self.template = self.cache[name]
72 self.name = name
73 else
74 self.name = "[string]"
75 end
76
77 -- Create a new namespace for this template
78 self.viewns = context.viewns
79
80 -- If we have a cached template, skip compiling and loading
81 if not self.template then
82
83 -- Compile template
84 local err
85 local sourcefile
86
87 if name then
88 sourcefile = viewdir .. "/" .. name .. ".htm"
89 self.template, _, err = tparser.parse(sourcefile)
90 else
91 sourcefile = "[string]"
92 self.template, _, err = tparser.parse_string(template)
93 end
94
95 -- If we have no valid template throw error, otherwise cache the template
96 if not self.template then
97 error("Failed to load template '" .. name .. "'.\n" ..
98 "Error while parsing template '" .. sourcefile .. "':\n" ..
99 (err or "Unknown syntax error"))
100 elseif name then
101 self.cache[name] = self.template
102 end
103 end
104 end
105
106
107 -- Renders a template
108 function Template.render(self, scope)
109 scope = scope or getfenv(2)
110
111 -- Put our predefined objects in the scope of the template
112 setfenv(self.template, setmetatable({}, {__index =
113 function(tbl, key)
114 return rawget(tbl, key) or self.viewns[key] or scope[key]
115 end}))
116
117 -- Now finally render the thing
118 local stat, err = util.copcall(self.template)
119 if not stat then
120 error("Failed to execute template '" .. self.name .. "'.\n" ..
121 "A runtime error occured: " .. tostring(err or "(nil)"))
122 end
123 end