- local viewns = {}
- tpl.context.viewns = viewns
- viewns.write = luci.http.write
- viewns.translate = function(...) return require("luci.i18n").translate(...) end
- viewns.striptags = util.striptags
- viewns.controller = luci.http.getenv("SCRIPT_NAME")
- viewns.media = luci.config.main.mediaurlbase
- viewns.resource = luci.config.main.resourcebase
- viewns.REQUEST_URI = (luci.http.getenv("SCRIPT_NAME") or "") .. (luci.http.getenv("PATH_INFO") or "")
- end
-
+ local media = track.mediaurlbase or luci.config.main.mediaurlbase
+ if not pcall(tpl.Template, "themes/%s/header" % fs.basename(media)) then
+ media = nil
+ for name, theme in pairs(luci.config.themes) do
+ if name:sub(1,1) ~= "." and pcall(tpl.Template,
+ "themes/%s/header" % fs.basename(theme)) then
+ media = theme
+ end
+ end
+ assert(media, "No valid theme found")
+ end
+
+ tpl.context.viewns = setmetatable({
+ write = luci.http.write;
+ include = function(name) tpl.Template(name):render(getfenv(2)) end;
+ translate = function(...) return require("luci.i18n").translate(...) end;
+ export = function(k, v) if tpl.context.viewns[k] == nil then tpl.context.viewns[k] = v end end;
+ striptags = util.striptags;
+ pcdata = util.pcdata;
+ media = media;
+ theme = fs.basename(media);
+ resource = luci.config.main.resourcebase
+ }, {__index=function(table, key)
+ if key == "controller" then
+ return build_url()
+ elseif key == "REQUEST_URI" then
+ return build_url(unpack(ctx.requestpath))
+ else
+ return rawget(table, key) or _G[key]
+ end
+ end})
+ end
+