1
0
mirror of https://github.com/lxsang/antd-web-apps synced 2024-11-20 02:18:20 +01:00
antd-web-apps/silk/Template.lua

37 lines
827 B
Lua
Raw Normal View History

2018-08-21 15:16:36 +02:00
-- create class
2018-08-22 19:38:36 +02:00
Template = BaseObject:extends{class="Template",registry = {}}
2018-08-21 15:16:36 +02:00
function Template:initialize()
self.vars = {}
end
function Template:set(k, v, ow)
2018-08-22 19:38:36 +02:00
if not self.vars[k] or (self.vars[k] and ow) then
self.vars[k] = v
end
2018-08-21 15:16:36 +02:00
end
function Template:remove(k)
2018-08-22 19:38:36 +02:00
self.vars[k] = nil
2018-08-21 15:16:36 +02:00
end
-- infer view path
2018-08-22 19:38:36 +02:00
function Template:setView(name, controller)
self.path = VIEW_ROOT..DIR_SEP..self.registry.layout..DIR_SEP..controller..DIR_SEP..name..".ls"
if ulib.exists(self.path) then
else
self:error("View not found: "..self.path)
end
2018-08-21 15:16:36 +02:00
end
-- render the page
2018-08-22 19:38:36 +02:00
function Template:render()
local fn, e = loadscript(self.path)
if fn then
local r,o = pcall(fn, self.vars)
if not r then
self:error(o)
end
else
self:error(e)
end
2018-08-21 15:16:36 +02:00
end