-context = luci.util.threadlocal()
-
-viewns = {
- include = function(name) Template(name):render(getfenv(2)) end,
-}
-
--- Compiles a given template into an executable Lua module
-function compile(template)
- -- Search all <% %> expressions (remember: Lua table indexes begin with #1)
- local function expr_add(ws1, skip1, command, skip2, ws2)
- table.insert(expr, command)
- return ( skip1 and "" or ws1 ) ..
- "<%" .. tostring(#expr) .. "%>" ..
- ( skip2 and "" or ws2 )
- end
-
- -- As "expr" should be local, we have to assign it to the "expr_add" scope
- local expr = {}
- luci.util.extfenv(expr_add, "expr", expr)
-
- -- Save all expressiosn to table "expr"
- template = template:gsub("(%s*)<%%(%-?)(.-)(%-?)%%>(%s*)", expr_add)
-
- local function sanitize(s)
- s = luci.util.escape(s)
- s = luci.util.escape(s, "'")
- s = luci.util.escape(s, "\n")
- return s
- end
-
- -- Escape and sanitize all the template (all non-expressions)
- template = sanitize(template)