MoldLive is an online playground where mold runs as WASM directly in your browser. Edit the template and JSON data side by side, then see the rendered output immediately.
mold 当前聚焦在一个明确范围内:把模板稳定解析成 AST,再基于统一的 Value 模型完成渲染。它优先服务于报告生成、邮件模板、配置文件生成、文档模板这类文本生成场景,同时也支持通过显式配置进入 HTML 输出场景。
mold currently focuses on a clear scope: parse templates into ASTs and render them against a unified Value model. It primarily targets text generation scenarios such as reports, email templates, config files, and document generation, while also supporting HTML output through explicit configuration.
The diagram below summarizes the core mold flow: templates and JSON data enter parsing and inspection, then rendering produces reports, emails, config files, or HTML.
安装 / Installation
moon add robinfang/mold
30 秒上手 / 30-Second Quick Start
let output = @mold.render(
"Hello, {{ name }}!",
@mold.object({ "name": @mold.string("World") }),
)
推荐使用路径 / Recommended Workflows
这三条路径对应 mold 的总体框架:
顶层函数:更轻,适合快速渲染
Template:更稳,适合 parse once, render many
Engine:更可扩展,适合 include、autoescape、自定义 filter
These three workflows map directly to the overall structure of mold: top-level rendering for quick use, Template for repeated rendering, and Engine for extensibility.
小模板或一次性渲染:@mold.render(...)
重复渲染同一模板:Template::parse(...).render(...)
需要 include、autoescape 或自定义 filter:Engine
Use @mold.render(...) for small templates or one-off rendering.
Use Template::parse(...).render(...) when the same template is rendered repeatedly.
Use Engine when you need include, autoescape, or custom filters.
When reading mold for the first time, start with this page for project scope, installation, and the three recommended workflows, then move to getting-started, and finally to template-syntax when you begin writing templates.
Template composition with Engine + Loader + include
src/examples/html_safe/
with_autoescape(true) 与 | safe
with_autoescape(true) and | safe
src/examples/custom_filter/
Engine::register_filter(...) 自定义 filter
Custom filters via Engine::register_filter(...)
src/examples/from_json/
from_json(...) 把 JSON 转成模板上下文
Convert JSON into template context with from_json(...)
src/examples/site/
静态网站批量渲染
Static site batch rendering
运行示例 / Run an example:
moon run src/examples/hello
moon run src/examples/report
moon run src/examples/email
moon run src/examples/include_loader
moon run src/examples/html_safe
moon run src/examples/custom_filter
moon run src/examples/from_json
moon run src/examples/site
结构化错误类型与源码位置 / structured errors with source spans
HTML 安全 / HTML Safety
默认情况下,mold 不自动转义 HTML,适合通用文本生成场景。输出 HTML 时,建议显式使用 Engine::with_autoescape(true)。
By default, mold does not autoescape HTML, which keeps it suitable for general text generation. For HTML output, explicitly enable Engine::with_autoescape(true).
mold intentionally keeps the template language small and predictable. It focuses on parsing, inspection, rendering, filters, includes, and WASM usage rather than becoming a full web framework.
mold
mold是一个面向 MoonBit 生态的轻量模板引擎。moldis a lightweight template engine for the MoonBit ecosystem.在线体验 / Live Demo
MoldLive 是一个在线模板游乐场,
mold编译为 WASM 在浏览器中直接运行。你可以在三栏编辑器里修改模板和 JSON 数据,并实时查看输出。MoldLive is an online playground where
moldruns as WASM directly in your browser. Edit the template and JSON data side by side, then see the rendered output immediately.mold语法高亮mold-live/子应用总体概况 / Overview
mold当前聚焦在一个明确范围内:把模板稳定解析成 AST,再基于统一的Value模型完成渲染。它优先服务于报告生成、邮件模板、配置文件生成、文档模板这类文本生成场景,同时也支持通过显式配置进入 HTML 输出场景。moldcurrently focuses on a clear scope: parse templates into ASTs and render them against a unifiedValuemodel. It primarily targets text generation scenarios such as reports, email templates, config files, and document generation, while also supporting HTML output through explicit configuration.核心链路 / Core Flow
下面这张图概括了
mold的核心链路:模板和 JSON 数据进入解析/诊断流程,再由渲染器产出报告、邮件、配置或 HTML。The diagram below summarizes the core
moldflow: templates and JSON data enter parsing and inspection, then rendering produces reports, emails, config files, or HTML.安装 / Installation
30 秒上手 / 30-Second Quick Start
推荐使用路径 / Recommended Workflows
这三条路径对应
mold的总体框架:Template:更稳,适合 parse once, render manyEngine:更可扩展,适合 include、autoescape、自定义 filterThese three workflows map directly to the overall structure of
mold: top-level rendering for quick use,Templatefor repeated rendering, andEnginefor extensibility.小模板或一次性渲染:
@mold.render(...)重复渲染同一模板:
Template::parse(...).render(...)需要 include、autoescape 或自定义 filter:
EngineUse
@mold.render(...)for small templates or one-off rendering.Use
Template::parse(...).render(...)when the same template is rendered repeatedly.Use
Enginewhen you need include, autoescape, or custom filters.阅读框架 / Reading Guide
第一次接触
mold时,建议按下面顺序阅读:docs/getting-started.md,把最小示例和运行方式跑通。docs/template-syntax.md。When reading
moldfor the first time, start with this page for project scope, installation, and the three recommended workflows, then move togetting-started, and finally totemplate-syntaxwhen you begin writing templates.文档导航 / Documentation
docs/index.mddocs/getting-started.mddocs/template-syntax.mddocs/engine-guide.mddocs/inspection.mddocs/html-safety.mddocs/errors.mddocs/wasm-export.mddocs/scope-proof.mddocs/recipes/report-generation.mddocs/recipes/email-template.mddocs/recipes/include-loader.mddocs/recipes/json-input.mddocs/recipes/site-generation.md示例 / Examples
src/examples/hello/src/examples/report/src/examples/email/src/examples/include_loader/Engine + Loader + include的模板组合Engine + Loader + includesrc/examples/html_safe/with_autoescape(true)与| safewith_autoescape(true)and| safesrc/examples/custom_filter/Engine::register_filter(...)自定义 filterEngine::register_filter(...)src/examples/from_json/from_json(...)把 JSON 转成模板上下文from_json(...)src/examples/site/运行示例 / Run an example:
MoldLive 本地运行 / Local MoldLive
MoldLive 是本仓库内的浏览器 Demo 子应用。先从当前
mold源码构建 WASM,再同步到前端应用:验证生产构建:
能力摘要 / Feature Summary
{{ expr }}插值与点路径访问 / interpolation with dotted lookup{% if %}/{% else %}/{% endif %}条件分支 / conditional blocks{% for %}/{% endfor %}循环,支持嵌套控制块 / loops with nested control blocksupperlowertrimdefault(...)join(...)escapelengthsafe== != < <= > >=and or not{% include "template_name" %}模板包含 / template inclusion{%-/-%}和{{-/-}}{# ... #}模板注释 / template commentsTemplate::ast()调试访问 / AST debug accessorTemplate::inspect()/inspect(...)模板依赖诊断 / template dependency inspectionfrom_json/from_map上下文转换 / context conversion helpersHTML 安全 / HTML Safety
默认情况下,
mold不自动转义 HTML,适合通用文本生成场景。输出 HTML 时,建议显式使用Engine::with_autoescape(true)。By default,
molddoes not autoescape HTML, which keeps it suitable for general text generation. For HTML output, explicitly enableEngine::with_autoescape(true).API 摘要 / API Snapshot
设计边界 / Design Boundaries
moldintentionally keeps the template language small and predictable. It focuses on parsing, inspection, rendering, filters, includes, and WASM usage rather than becoming a full web framework.当前有意不覆盖 / intentionally out of scope for now:
仓库链接 / Repository Links
发布状态 / Release Status
mold已发布到mooncakes.io,当前版本为0.3.0。moldis now published onmooncakes.io, and the current version is0.3.0.开源协议 / License
Apache-2.0