summaryrefslogtreecommitdiffhomepage
path: root/libs/web/luasrc/template.lua
diff options
context:
space:
mode:
authorSteven Barth <steven@midlink.org>2008-09-05 18:35:03 +0000
committerSteven Barth <steven@midlink.org>2008-09-05 18:35:03 +0000
commit37b5ef40df48bb7d4023e66f09a68c1c0e229b8d (patch)
tree916085901acc739dd0050488ec893bbfec4bda35 /libs/web/luasrc/template.lua
parent45319b232bc331492dd98e474c9ea60a9a70bc8f (diff)
Heavy memory/performance optimizations #3
Diffstat (limited to 'libs/web/luasrc/template.lua')
-rw-r--r--libs/web/luasrc/template.lua28
1 files changed, 6 insertions, 22 deletions
diff --git a/libs/web/luasrc/template.lua b/libs/web/luasrc/template.lua
index 91977798a..4aa9b0933 100644
--- a/libs/web/luasrc/template.lua
+++ b/libs/web/luasrc/template.lua
@@ -34,7 +34,7 @@ local coroutine = require "coroutine"
local tostring, pairs, loadstring = tostring, pairs, loadstring
local setmetatable, loadfile = setmetatable, loadfile
-local getfenv, setfenv = getfenv, setfenv
+local getfenv, setfenv, rawget = getfenv, setfenv, rawget
local assert, type, error = assert, type, error
--- LuCI template library.
@@ -55,10 +55,6 @@ compiler_mode = config.template.compiler_mode or "memory"
-- Define the namespace for template modules
context = util.threadlocal()
-viewns = {
- include = function(name) Template(name):render(getfenv(2)) end,
-}
-
--- Manually compile a given template into an executable Lua function
-- @param template LuCI template
-- @return Lua template function
@@ -160,13 +156,7 @@ function Template.__init__(self, name)
self.name = name
-- Create a new namespace for this template
- self.viewns = {sink=self.sink}
-
- -- Copy over from general namespace
- util.update(self.viewns, viewns)
- if context.viewns then
- util.update(self.viewns, context.viewns)
- end
+ self.viewns = context.viewns
-- If we have a cached template, skip compiling and loading
if self.template then
@@ -235,21 +225,15 @@ end
function Template.render(self, scope)
scope = scope or getfenv(2)
- -- Save old environment
- local oldfenv = getfenv(self.template)
-
-- Put our predefined objects in the scope of the template
- util.resfenv(self.template)
- util.updfenv(self.template, scope)
- util.updfenv(self.template, self.viewns)
+ setfenv(self.template, setmetatable({}, {__index =
+ function(tbl, key)
+ return rawget(tbl, key) or self.viewns[key] or scope[key]
+ end}))
-- Now finally render the thing
local stat, err = util.copcall(self.template)
if not stat then
- setfenv(self.template, oldfenv)
error("Error in template %s: %s" % {self.name, err})
end
-
- -- Reset environment
- setfenv(self.template, oldfenv)
end