From 188d052d34f9a6f6d15295d4b0615900a626d5c3 Mon Sep 17 00:00:00 2001 From: rxi Date: Sun, 13 Apr 2014 15:48:24 +0100 Subject: [PATCH] Changed lovebird.template() to return compiled template lovebird.template() now returns the compiled template as a function rather than the result of the template. To make this possible the parameters and usage of the function has also changed. --- lovebird.lua | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/lovebird.lua b/lovebird.lua index 6e65866..0978a53 100644 --- a/lovebird.lua +++ b/lovebird.lua @@ -393,20 +393,18 @@ function lovebird.init() end -function lovebird.template(str, env) - env = env or {} - local keys, vals = {}, {} - for k, v in pairs(env) do - table.insert(keys, k) - table.insert(vals, v) - end +function lovebird.template(str, params) + params = params and ("," .. params) or "" local f = function(x) return string.format(" echo(%q)", x) end str = ("?>"..str.."(.-)<%?lua", f) - str = "local echo, " .. table.concat(keys, ",") .. " = ..." .. str - local output = {} - local echo = function(str) table.insert(output, str) end - assert(loadstring(str))(echo, unpack(vals)) - return table.concat(map(output, tostring)) + str = "local echo " .. params .. " = ..." .. str + local fn = assert(loadstring(str)) + return function(...) + local output = {} + local echo = function(str) table.insert(output, str) end + fn(echo, ...) + return table.concat(map(output, tostring)) + end end @@ -501,7 +499,7 @@ function lovebird.onrequest(req, client) xpcall(function() str = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n" .. lovebird.template(lovebird.pages[page], - { lovebird = lovebird, req = req }) + "lovebird, req")(lovebird, req) end, lovebird.onerror) return str end