moon add daqing/moon-templmoon build --target native cmd/moon-templ
install -m 755 _build/native/debug/build/cmd/moon-templ/moon-templ.exe /usr/local/bin/moon-templtempl hello(name : String) {
<div class="greeting">Hello, { name }!</div>
}moon-templ generate hello.templ
# generated hello_templ.mbtimport {
"daqing/moon-templ/runtime" @rt,
}
formatter(ignore: [ "hello_templ.mbt" ])///|
fn main {
println(hello("world").to_string())
}<div class="greeting">Hello, world!</div>templ card(title : String, id : String, locked : Bool, dark : Bool, attrs : @rt.Attributes) {
<article class="card" data-id={ id } disabled?={ locked } { attrs... }
if dark { data-theme="dark" } else { data-theme="light" }>
<h2>{ title }</h2>
{ children... }
</article>
}if ok {
<p>ready</p>
} else if retrying {
<p>trying again</p>
} else {
<p>stopped</p>
}
for item in items {
<li>{ item }</li>
}
for i = 0; i < count; i = i + 1 {
<span>{ i }</span>
}
match kind {
Some(v) => {
<em>{ v }</em>
}
_ => {
<em>none</em>
}
}moon-templ generate <file.templ>
moon-templ generate <dir>... # or a bare directory
moon-templ generate --watch <dir>...
moon-templ fmt <file.templ> # or <dir>...rule(
name: "templ",
command: "moon-templ generate $input",
)
dev_build(
rule: "templ",
input: "hello.templ",
output: "hello_templ.mbt",
)| templ | moon-templ |
|---|---|
| Go | MoonBit |
| switch / case | match with MoonBit patterns: Some(v) => { ... }, _ => { ... } |
| for i, v := range items | for v in items; C-style for i = 0; i < n; i = i + 1 { ... } also works |
| {{ go code }} | {{ MoonBit code }}, copied into the generated function body |
| templ Name() with any Go name | The template becomes a MoonBit function, so its name must start with a lowercase letter or _ |
| templ (t T) Name() | The same receiver syntax, with the same naming rule |
| @Component() / @pkg.Component() | @component() for a sibling, @pkg.component(...) for another package |
| _templ.go | _templ.mbt |
| templ generate --watch with a live-reload proxy | --watch only regenerates; native target only |
| script declarations and RenderScriptItems | Not implemented yet; <script> elements are kept verbatim and @rt.ComponentScript renders a call string |
moon check --deny-warn
moon test --deny-warn
moon fmt
moon info