1
0
mirror of https://github.com/lxsang/antd-web-apps synced 2024-12-27 09:58:20 +01:00
antd-web-apps/silk/Template.lua

58 lines
1.2 KiB
Lua
Raw Normal View History

2018-08-21 15:16:36 +02:00
-- create class
2018-08-27 20:10:53 +02:00
BaseObject:subclass("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:get(k)
return self.vars[k]
end
2018-08-21 15:16:36 +02:00
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.name = name
if controller then
self.controller = controller
end
end
function Template:path()
local path = VIEW_ROOT..DIR_SEP..self.registry.layout..DIR_SEP..self.controller..DIR_SEP..self.name..".ls"
if ulib.exists(path) then
return path
2018-08-22 19:38:36 +02:00
else
2018-09-05 16:56:04 +02:00
return false, path
2018-08-22 19:38:36 +02:00
end
2018-08-21 15:16:36 +02:00
end
-- render the page
2018-08-22 19:38:36 +02:00
function Template:render()
2018-09-05 16:56:04 +02:00
local path, err = self:path()
if not path then
return self:error("View not found: "..err)
end
local args = {}
local argv = {}
for k, v in pairs(self.vars) do
table.insert( args, k )
table.insert( argv,v )
end
local fn, e = loadscript(self:path(), args)
2018-08-22 19:38:36 +02:00
if fn then
2018-09-05 16:56:04 +02:00
local r,o = pcall(fn, table.unpack(argv))
2018-08-22 19:38:36 +02:00
if not r then
self:error(o)
end
else
self:error(e)
end
2018-08-21 15:16:36 +02:00
end