function Astra.new_templating_engine(dir)
local engine = astra_internal__new_templating_engine(dir)
local TemplateEngineWrapper = { engine = engine }
local templates_re = Astra.regex([[(?:index)?\.(html|lua)$]])
local function normalize_paths(path)
if path:sub(1, 1) ~= "/" then
path = "/" .. path
end
if path == "/" then
return { "/" }
end
if path:sub(-1) == "/" then
return { path, path:sub(1, -2) }
else
return { path, path .. "/" }
end
end
function TemplateEngineWrapper:add_to_server(server, context)
local names = self.engine:get_template_names()
for _, value in ipairs(names) do
local path = templates_re:replace(value, "")
local content = self.engine:render(value, context)
for _, route in ipairs(normalize_paths(path)) do
server:get(route, function(_, response)
response:set_header("Content-Type", "text/html")
return content
end)
end
end
end
function TemplateEngineWrapper:add_to_server_debug(server, context)
local names = self.engine:get_template_names()
for _, value in ipairs(names) do
local path = templates_re:replace(value, "")
for _, route in ipairs(normalize_paths(path)) do
server:get(route, function(_, response)
self.engine:reload_templates()
response:set_header("Content-Type", "text/html")
return self.engine:render(value, context)
end)
end
end
end
local templating_methods = {
"add_template",
"add_template_file",
"get_template_names",
"exclude_templates",
"reload_templates",
"context_add",
"context_remove",
"context_get",
"add_function",
"render",
}
for _, method in ipairs(templating_methods) do
TemplateEngineWrapper[method] = function(self, ...)
return self.engine[method](self.engine, ...)
end
end
return TemplateEngineWrapper
end