示例工程 34-mini-dsl

34-mini-dsl/02-template-engine/main.lua

lua · 0.5 KB

-- @runtimes lua54 luajit
--- 演示:用 gsub 实现 ${name} 模板。
local M = {}
 
local function render(template, values)
	return (template:gsub("%${(%w+)}", function(key)
		return values[key] or ""
	end))
end
 
--- @return table { text = string }
function M.run(log)
	log = log or print
	local text = render("hello ${name}, you are ${age}", { name = "Ada", age = 36 })
	log(("text=%s"):format(text))
	return { text = text }
end
if not ... then
	M.run()
end
return M