作者曾嘗試自製 Smarty 風格模板引擎(HTML 模板產生 ASPX),但複雜度過高而放棄。後發現 DotLiquid(官網),源自 Ruby Liquid Markup 移植,標榜「易學、快速、安全」。
{{ variable }}、{% if %}、{% for item in list %}。// 模板字串
string template = "<div>{{ title }}</div><ul>{% for u in users %}<li>{{ u.Name }}</li>{% endfor %}</ul>";
// 資料模型
var model = new { title = "用戶列表", users = new[] { new { Name = "Tom" }, new { Name = "Jerry" } } };
// 渲染
string result = Template.Parse(template).Render(Hash.FromAnonymousObject(model));
DotLiquid 為 ASP.NET 專案引入成熟、安全的模板引擎,適合郵件範本、報表產生、動態頁面渲染等場景,避免自行開發維護成本。