Хабр Курсы для всех
РЕКЛАМА
Практикум, Хекслет, SkyPro, авторские курсы — собрали всех и попросили скидки. Осталось выбрать!
function template(G) G=G or _G return function(t)
local r=t:gsub('{{([^}]+)}}',function(v)
local g=G for i in v:gmatch("[^%.]+") do
local p,fn=i:find('|')
if p then fn=i:sub(p+1) i=i:sub(1,p-1) end
if type(g[i])=='function' then g=g[i]() else g=g[i]
if g==nil then error("no variable "..v) end
end
if p then fn:gsub('[^|]+',function(f)
if G[f]==nil then error("no function "..fn) end
g=G[f](g)
end)
end
end
return g
end)
return r
end
end
function xml_encode(x)
local tab={ ['<']='<',['>']='>',
['&']='&',['"']='"',["'"]=''' }
return x:gsub("[<>&'\"]",function(v) return tab[v] end)
end
t=template{e=xml_encode, x=10, y="<&>"}
print( t[[
x={{x}}
y={{y|e}}
]] )
x=10
y=<&>
Jinja2 в мире C++, часть вторая. Рендеринг