- local viewns = setmetatable({}, {__index=_G})
- tpl.context.viewns = viewns
- viewns.write = luci.http.write
- viewns.include = function(name) tpl.Template(name):render(getfenv(2)) end
- viewns.translate = function(...) return require("luci.i18n").translate(...) end
- viewns.striptags = util.striptags
- viewns.controller = luci.http.getenv("SCRIPT_NAME")
- viewns.media = media
- viewns.theme = fs.basename(media)
- viewns.resource = luci.config.main.resourcebase
- viewns.REQUEST_URI = (luci.http.getenv("SCRIPT_NAME") or "") .. (luci.http.getenv("PATH_INFO") or "")
+ local function _ifattr(cond, key, val)
+ if cond then
+ local env = getfenv(3)
+ local scope = (type(env.self) == "table") and env.self
+ return string.format(
+ ' %s="%s"', tostring(key),
+ luci.util.pcdata(tostring( val
+ or (type(env[key]) ~= "function" and env[key])
+ or (scope and type(scope[key]) ~= "function" and scope[key])
+ or "" ))
+ )
+ else
+ return ''
+ end
+ end
+
+ tpl.context.viewns = setmetatable({
+ write = luci.http.write;
+ include = function(name) tpl.Template(name):render(getfenv(2)) end;
+ translate = i18n.translate;
+ translatef = i18n.translatef;
+ 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;
+ ifattr = function(...) return _ifattr(...) end;
+ attr = function(...) return _ifattr(true, ...) end;
+ }, {__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})