A Handlebars-style template engine for MoonBit. It supports variable interpolation, raw output, conditionals, unless, loops, comments, and safe-by-default HTML escaping.
Package metadata
Field
Value
MoonBit package
jshsj124/moontemplate
Version
0.1.1
Repository
https://github.com/jshsj124/MoonTemplate
License
MIT
The package name above matches moon.mod; publishing and importing must use this exact namespace. Older namespaces are not substitutes.
///|
fn main {
let template = "<h1>{{ title }}</h1>\n" +
"<ul>{{#each items}}<li>{{ this }}</li>{{/each}}</ul>"
let ctx = [
("title", "MoonTemplate <demo>"),
("items", "lexer,parser,renderer"),
]
let html = @moontemplate.render_template(template, ctx)
println(html)
}
Escape HTML-sensitive characters: &, <, >, ", ', and backtick.
Context is an Array[(String, String)] of variable name-value pairs.
Template features
Variable interpolation: {{ name }}
Raw/unescaped output: {{{ html }}}
Conditionals: {{#if show}}visible{{/if}}
If/else: {{#if flag}}yes{{else}}no{{/if}}
Unless: {{#unless hide}}visible{{/unless}}
Loops: {{#each items}}{{ this }}{{/each}}
Comments: {{! inline }} and {{!-- block --}}
Automatic HTML escaping for normal interpolation
Development checks
moon check --deny-warn
moon fmt --check
moon info
git diff --exit-code -- '**/pkg.generated.mbti'
moon test --deny-warn
MoonBit 2026-07 no longer accepts --deny-warn on moon fmt or moon info; the commands above are the current toolchain equivalents. CI feature-detects older toolchains and runs the strict form when available.
MoonTemplate
A Handlebars-style template engine for MoonBit. It supports variable interpolation, raw output, conditionals,
unless, loops, comments, and safe-by-default HTML escaping.Package metadata
jshsj124/moontemplate0.1.1https://github.com/jshsj124/MoonTemplateInstallation
Add the package to your app’s
moon.pkg:Complete runnable example
Create
cmd/main/moon.pkg:Create
cmd/main/main.mbt:Run it:
Expected output:
API
render_template(template, ctx)compile(template) -> @types.Noderender(ast, ctx) -> Stringrender_enhanced(template, ctx, partials, helpers)escape_html(text)&,<,>,",', and backtick.Context is an
Array[(String, String)]of variable name-value pairs.Template features
{{ name }}{{{ html }}}{{#if show}}visible{{/if}}{{#if flag}}yes{{else}}no{{/if}}{{#unless hide}}visible{{/unless}}{{#each items}}{{ this }}{{/each}}{{! inline }}and{{!-- block --}}Development checks
MoonBit 2026-07 no longer accepts
--deny-warnonmoon fmtormoon info; the commands above are the current toolchain equivalents. CI feature-detects older toolchains and runs the strict form when available.License
MIT