A constant template is finished during compilation.
title #= "Moth"
heading #= [$md:
# [title]
]
A bare direct [...] in @entry.moth must fold at compile time. An explicit ~[...] instead creates a runtime body fragment, even if foldable. The removed direct #[...] spelling is invalid. The entry file's presence selects the page. A direct const root $in insert supplies builder metadata, while an ordinary const fragment renders into the body.
[$in title:About]
[$in stylesheets:[@assets/site.css]]
[$md:
# Compile-time heading
]
Only a direct root insert reaches builder routing. Nested and stored inserts remain ordinary template composition. Scalar builder values concatenate, and each stylesheet insert needs one tracked CSS resource. The HTML builder deduplicates stylesheets by resource origin.
Runtime top-level named builder inserts are not supported.
Assigned or returned templates remain ordinary values, not direct page fragments.
Const template loops have an iteration limit so an accidental build-time loop cannot run forever.
HTML pages come from directories that contain @entry.moth. Direct const root inserts supply builder metadata.
A template can be required to finish during compilation.
Named const templates
A normal constant binding can hold a folded template string.
site_name #= "Moth"
heading #= [$md:
# [site_name]
]
This creates a compile-time String value. A template assigned to a binding or returned from a function remains a value and does not become a page fragment by itself.
Direct top-level const fragments
Every bare direct [...] fragment in @entry.moth is const-required by default. It must fold even if an ordinary template expression in the same position would be runtime-dependent. Use direct ~[...] to request a runtime body fragment. The file's presence makes its directory entry-eligible, so metadata-only pages remain valid. The removed direct #[...] spelling has no compatibility alias.
[$md:
# Compile-time heading
]
The bare direct form in the entry file:
- is valid only as top-level entry fragment syntax
- must fully fold, reporting a constant-required diagnostic for runtime dependence
- records its position relative to explicit
~[...] runtime fragments - is handed to the project builder
- never becomes HIR runtime code
Builder slot contributions
A builder can register named destinations for direct top-level const inserts. The HTML builder registers title, description, lang, favicon, body_style, stylesheets and head. Ordinary direct const fragments still render into the body. A metadata-only @entry.moth still produces a complete page.
[$in title:Moth]
[$in description:A language for creating reliable software]
[$in lang:en]
[$in favicon:[@assets/favicon.svg]]
[$in stylesheets:[@assets/site.css]]
[$in head:<meta name="theme-color" content="#ffffff">]
[:<main>Hello Moth</main>]
The HTML destinations are:
title: escaped title text, with the builder's route then project-name fallbackdescription: escaped meta description text, omitted when absentlang: escaped <html lang> value, with the builder default when absentfavicon: escaped icon URL from a tracked resource or a permitted Stringbody_style: escaped body style attribute, with the builder default when absentstylesheets: one tracked local CSS resource per insert, linked in first-origin orderhead: raw additional head HTML, appended after the builder's dedicated head content- the implicit default destination: ordered body fragments
A direct named insert must fold to a compile-time value. ~[$in name: ...] is not a runtime named builder contribution. A nonconstant contribution and a name the builder has not registered are diagnosed at their authored source. Nested, stored and aliased inserts remain ordinary template composition and never become builder messages. Ordinary template slots are owned by Template slots. Helper files, imported roots and API-only roots cannot contribute to the selected entry.
The builder interprets each registered destination. Scalar metadata contributions concatenate in source order and a supplied empty value stays present, even for title. There is no scalar cardinality error. Template body whitespace remains part of each value, including a space immediately after the body colon. A missing value uses the builder fallback, which is separate from an ordinary slot occurrence default. An absent title uses the route then project name with the configured prefix and postfix. An absent description emits no element. Other absent scalar values use the builder defaults. The builder escapes scalar metadata and never treats it as raw HTML.
Each stylesheets contribution must contain exactly one tracked local CSS resource. Plain paths, external URLs, site-root values, non-CSS files and combined resources are invalid. Use head for an external stylesheet. The builder keeps the first contribution for each stable resource origin and renders the link relative to each output page. head is raw HTML emitted after the builder's dedicated head content and deduplicated only when complete contributions render identically in the page URL context. It never replaces or bypasses scalar escaping. The full HTML contract is owned by Entry metadata and builder slots.
Named contributions keep their structural resources live, including on a page with no runtime work. Ordinary constants alone do not create builder metadata.
Const template if
[if show_heading:
Visible heading
]
A const template if conditionally includes one body when its Bool condition is true. It can also test an option with is |name|, making the captured value available only inside the selected body. A false condition or absent option produces structural no-output. Template if has no else or else if form.
The selector and body must be compile-time evaluable even when the body is not selected. A runtime expression in an inactive body is rejected. Only a selected body contributes output.
Const template loops
[loop items |item|:
[item]
]
Const collection and range loops expand their body for each visited iteration until the ordinary loop header ends. Template loops have no break or continue controls. Their body must fold on every expansion.
Const loops follow the ordinary loop header contract. An explicit step must be compile-time known. A runtime-only step is rejected with MOTH-SYNTAX-0022 (invalid_template_structure.template_loop_step_not_const). The step's sign selects direction, a negative collection step starts at the last source item and a collection loop's second binding is the actual source index.
The project config field template_const_loop_iteration_limit guards expansion:
template_const_loop_iteration_limit
The default is 10_000.
The maximum accepted configured value is 1_000_000.
The configured value must be a positive folded I32. Zero, negative values and values above 1_000_000 are rejected.
The limit counts visited iterations, not source positions. A literal zero step is rejected by the loop header with MOTH-SYNTAX-0029. A step that folds to zero is rejected with MOTH-SYNTAX-0022 (invalid_template_structure.template_loop_step_is_zero), even when the source is empty or the range would visit nothing. A const float range step that rounds back to the current counter is rejected with MOTH-SYNTAX-0022 (invalid_template_structure.template_loop_step_no_progress).
A const conditional loop folds to no-output only when its condition is compile-time false.
A compile-time true condition and a runtime or unknown condition are rejected because the compiler does not prove loop termination.
Runtime template control flow remains owned by Template control flow.
MON data boundary
A folded const template is an ordinary String that an encoder can quote. An unevaluated template is not a serialisable value. Compile time versus runtime concerns the producer and never changes document validity. The MON data format is independent of the producer and its runtime environment. See MON format.