X-Git-Url: https://git.archive.openwrt.org/?p=project%2Fluci.git;a=blobdiff_plain;f=libs%2Fweb%2Fluasrc%2Ftemplate.lua;h=17e1daad70b83e9c6f74dafce5340fcc558189d3;hp=dc0ccf6201895b1329a3860cb582dfe42f234636;hb=5b43543226fa29dc7d899e9fc82f0179aefcb56b;hpb=70b119b8eeda1a256eb54cd58c5070cf4b5a1b56 diff --git a/libs/web/luasrc/template.lua b/libs/web/luasrc/template.lua index dc0ccf620..17e1daad7 100644 --- a/libs/web/luasrc/template.lua +++ b/libs/web/luasrc/template.lua @@ -23,6 +23,8 @@ See the License for the specific language governing permissions and limitations under the License. ]]-- + +--- LuCI template library. module("luci.template", package.seeall) require("luci.config") @@ -50,9 +52,11 @@ viewns = { include = function(name) Template(name):render(getfenv(2)) end, } --- Compiles a given template into an executable Lua module +--- Manually compile a given template into an executable Lua function +-- @param template LuCI template +-- @return Lua template function function compile(template) - -- Search all <% %> expressions (remember: Lua table indexes begin with #1) + -- Search all <% %> expressions local function expr_add(ws1, skip1, command, skip2, ws2) table.insert(expr, command) return ( #skip1 > 0 and "" or ws1 ) .. @@ -68,10 +72,8 @@ function compile(template) 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 + s = string.format("%q", s) + return s:sub(2, #s-1) end -- Escape and sanitize all the template (all non-expressions) @@ -116,7 +118,9 @@ function compile(template) return loadstring(template) end --- Oldstyle render shortcut +--- Render a certain template. +-- @param name Template name +-- @param scope Scope to assign to template function render(name, scope, ...) scope = scope or getfenv(2) local s, t = luci.util.copcall(Template, name)