libs/web: move ifattr() and attr() helpers into dispatcher scope to make them avilabl...
authorJo-Philipp Wich <jow@openwrt.org>
Wed, 26 Oct 2011 02:17:45 +0000 (02:17 +0000)
committerJo-Philipp Wich <jow@openwrt.org>
Wed, 26 Oct 2011 02:17:45 +0000 (02:17 +0000)
libs/web/luasrc/cbi.lua
libs/web/luasrc/dispatcher.lua

index 86658d7..8b9805f 100644 (file)
@@ -1325,29 +1325,8 @@ end
 function AbstractValue.render(self, s, scope)
        if not self.optional or self.section:has_tabs() or self:cfgvalue(s) or self:formcreated(s) then
                scope = scope or {}
 function AbstractValue.render(self, s, scope)
        if not self.optional or self.section:has_tabs() or self:cfgvalue(s) or self:formcreated(s) then
                scope = scope or {}
-               scope.section   = s
-               scope.cbid      = self:cbid(s)
-               scope.striptags = luci.util.striptags
-               scope.pcdata    = luci.util.pcdata
-
-               scope.ifattr = function(cond,key,val)
-                       if cond then
-                               return string.format(
-                                       ' %s="%s"', tostring(key),
-                                       luci.util.pcdata(tostring( val
-                                        or scope[key]
-                                        or (type(self[key]) ~= "function" and self[key])
-                                        or "" ))
-                               )
-                       else
-                               return ''
-                       end
-               end
-
-               scope.attr = function(...)
-                       return scope.ifattr( true, ... )
-               end
-
+               scope.section = s
+               scope.cbid    = self:cbid(s)
                Node.render(self, scope)
        end
 end
                Node.render(self, scope)
        end
 end
index c1e8d99..b1ed62b 100644 (file)
@@ -281,6 +281,20 @@ function dispatch(request)
                        assert(media, "No valid theme found")
                end
 
                        assert(media, "No valid theme found")
                end
 
+               local function ifattr(cond, key, val)
+                       if cond then
+                               local env = getfenv(1)
+                               return string.format(
+                                       ' %s="%s"', tostring(key),
+                                       luci.util.pcdata(tostring( val
+                                        or (type(env[key]) ~= "function" and env[key])
+                                        or "" ))
+                               )
+                       else
+                               return ''
+                       end
+               end
+
                tpl.context.viewns = setmetatable({
                   write       = luci.http.write;
                   include     = function(name) tpl.Template(name):render(getfenv(2)) end;
                tpl.context.viewns = setmetatable({
                   write       = luci.http.write;
                   include     = function(name) tpl.Template(name):render(getfenv(2)) end;
@@ -290,7 +304,9 @@ function dispatch(request)
                   pcdata      = util.pcdata;
                   media       = media;
                   theme       = fs.basename(media);
                   pcdata      = util.pcdata;
                   media       = media;
                   theme       = fs.basename(media);
-                  resource    = luci.config.main.resourcebase
+                  resource    = luci.config.main.resourcebase;
+                  ifattr      = ifattr;
+                  attr        = function(...) return ifattr(true, ...) end
                }, {__index=function(table, key)
                        if key == "controller" then
                                return build_url()
                }, {__index=function(table, key)
                        if key == "controller" then
                                return build_url()