Moth template files

A .mtf file puts Markdown directly inside Moth's compile-time template system. It needs no frontmatter, embedded script block or separate template engine.

The compiler exports the folded result as content. Use plain .md for portable Markdown and normal .moth when the file needs declarations, dependencies or runtime work.

Choose the explanation level: Moth template files

Moth template files

A .mtf file is Markdown that lives inside Moth's template world.

Write prose directly

# Getting started

This is **bold** text.

[p: This paragraph uses the Core HTML p helper.]

No dependency clauses, no declarations, no frontmatter. Content only.

Bind the result

@docs/intro content as intro

[: [intro] ]

The content constant is the only thing a .mtf file exports.

A .mtf file is a Moth template file. The compiler prepares it as an implicit compile-time Markdown template body and exposes one generated content constant.

Implicit template structure

The compiler builds each .mtf file as if its whole body were the body of an implicit compile-time Markdown template:

content #String = [$md:
    ...entire .mtf file body...
]

The compiler constructs this structure directly from the token stream. It does not prepend wrapper source text to the file.

Exported declaration

A .mtf file exposes exactly one generated constant: content #String. The content is compile-time folded HTML text. No other declaration is exported.

File is content-oriented

A .mtf file is not a normal declaration file. It does not contain authored dependency clauses, declarations, frontmatter or metadata. It is a template body that produces a folded string.

Comments and punctuation

Because the file body is a template body, -- is content text, not a Moth comment. Backslashes follow template-body formatter behaviour. Backticks are Markdown inline-code delimiters, not raw string syntax. A $note child written in the body is valid and discards its balanced content.

Source file path and module ownership

A .mtf file belongs to its surrounding module. Same-module files can bind its generated content constant. A public facade can re-export it using $export on a direct-selection dependency clause.

Target and builder availability

Moth template files are compiler-supported under every builder, including custom SDK builders. The .mtf body sees a restricted flat compile-time scope provided by the compiler. See Template scope.

Concepts related to Moth template files

Choose the explanation level: Implicit Markdown

Implicit Markdown

Moth template files use a small Markdown subset, not full CommonMark.

What works

  • Headings with #, plus explicit @#id section IDs; a marker-only paragraph renders a standalone empty span
  • Plain .md does not use section markers
  • Paragraphs separated by blank lines
  • *italic* and **bold**
  • Links
  • Unordered and ordered lists
  • Inline code with single backticks

What does not work

  • Fenced code blocks (triple backticks). Use codeblock helpers instead.
  • Pipe tables. Use structured lists instead.
  • Raw HTML is escaped. Use $raw or $html directives for raw output.

Moth template files use Moth's $md Markdown flavour. This is intentionally smaller than full CommonMark.

Supported Markdown

  • Headings: #, ##, ### and deeper
  • Paragraphs: separated by blank lines
  • Emphasis: *italic* and **bold**
  • Moth-aware links: @path (label)
  • Unordered lists: - item
  • Ordered lists: 1. item
  • Inline code: paired isolated single backticks on the same line
  • Section IDs: a bare @#id marker on a heading, paragraph or list item

A heading, paragraph or list item can carry a bare @#id marker for an explicit HTML ID. A marker-only paragraph renders a standalone empty span, while a labelled @#id (label) remains a link. Plain .md does not use section markers. See Explicit section IDs.

HTML escaping

Moth template files escape HTML characters in Markdown text. Raw HTML tags such as <div> are escaped as &lt;div&gt; in the output. Use $raw or $html directives when raw HTML output is needed.

Not supported

  • Fenced code blocks (triple backtick blocks) are not rendered as <pre><code>. They appear as literal text. Use a codeblock helper with a $code "lang" operand instead.
  • Markdown pipe tables are not rendered as <table>. They appear as literal text. Use a structured list or a page-owned table component instead.
  • Full CommonMark is not claimed. Moth's $md is a small, deliberate subset.

Backticks are inline-code delimiters

In $md, backticks delimit inline code, not raw strings. Empty spans, repeated backtick runs, unmatched backticks, multiline spans, variable-length delimiters and fenced code blocks are not part of the $md flavour.

Backticks do not escape Moth template brackets. To display literal template syntax in inline code, insert a quoted String. For example, ~[...] displays the runtime-fragment syntax. See Markdown formatting for the full example.

Links use Moth-aware syntax

Use @/docs/packages (Packages) for internal routes or @https://example.com (Example) for external links. Ordinary [label](path) Markdown links belong to Plain Markdown and aren't parsed as links by a moth template's $md formatter.

A target beginning with a single / is a site route and renders through the site root, so the configured origin is applied for you. Relative, fragment, protocol-relative and absolute targets render literally. See Markdown formatting.

-- is content

Because the file body is a template body, -- is body text, not a Moth comment. It renders as literal -- in the output.

Backslashes follow body behaviour

Backslashes in the template body follow formatter behaviour. They are not escape characters in the same sense as normal Moth source.

Code examples

Use codeblock and $code "lang" where the helper is available through the content environment. See Template scope for what is visible inside a .mtf file.

Concepts related to implicit Markdown

Choose the explanation level: Content dependencies

Content dependencies

Bind Moth template content with normal extensionless dependency clauses.

Bind and use

@docs/intro content as intro

[: [intro] ]

The content constant is a compile-time string. Insert it wherever you want.

Do not include the extension

@docs/intro.mtf content is rejected. Use @docs/intro content.

Moth template content is bound with normal extensionless source dependency clause syntax. The bound content constant is a compile-time string.

Dependency syntax

@docs/intro

[: [intro.content] ]

A bare namespace clause exposes content as a field. A direct selection binds it directly:

@docs/intro content as intro_content

[: [intro_content] ]

Aliasing content

Use as to give the bound content a local name. This is the standard form when multiple content files are depended on by the same page.

Compile-time string semantics

The bound content is a #String compile-time constant. It has already folded by the time the declaring file uses it. It is not a runtime value.

Use in a page or template

Insert the bound content into a template body:

@docs/intro content as intro

[: [intro] ]

The caller decides where to insert it. Bound content does not become a page fragment automatically.

Explicit extensions are rejected for source content

@docs/intro.mtf content

This is rejected with MOTH-IMPORT-0024. Use @docs/intro without the .mtf extension.

The same content as a file value

The explicit-extension spelling that a clause rejects is valid in expression position, where it evaluates to the same content string:

intro #= @docs/intro.mtf

Both forms name one compiled file and one content value. Use the clause when the bound name matters or the file is used repeatedly, and the direct file value when a template needs the content once. See File values.

Relative path resolution

Content dependency clauses follow the same path resolution rules as normal source dependency clauses. Both @intro and @docs/intro resolve from the declaring file's owning module root; the latter selects docs/intro below that root. @./... has no supported meaning.

Dependencies from root or helper files

A .mtf file can be depended on from a module root, a normal source file, or both. The dependency path resolves the same way regardless of the declaring file's role.

Bound content does not become a page fragment

Binding a .mtf file and inserting its content into a template does not create a page fragment. The caller controls where the content appears. Only direct top-level templates in the entry-selected module root contribute page output or builder metadata.

Visibility and module boundary

A .mtf file's content follows ordinary module visibility. To make it accessible across a module boundary, re-export it from the relevant facade:

$export
@intro content as intro

Concepts related to content dependencies

Choose the explanation level: Template scope

Template scope

A .mtf file sees the fixed @core/html and @core/css helpers and values from helper files in the same module. It does not see module root declarations.

Selecting a helper value

-- docs/settings.moth
site_name #= "Moth"
-- docs/intro.mtf
# Welcome to [@settings site_name]

The path names the helper file and the symbol names one compile-time value. Keep them on one line. No @mod.moth file is needed.

The selected name does not become a local name. It cannot clash with a prelude helper.

Root files stay private

A value written in @mod.moth, @package.moth or @entry.moth is not visible to a .mtf file, even when the root exports it. Move shared values into an ordinary helper file and select them with a qualified reference.

The fixed helpers

.mtf files see constants from @core/html and @core/css without a dependency clause:

[p: A paragraph.]
[h1, center: A centred heading.]

That prelude comes from the official Core @core/html and @core/css packages. It is available under every builder, not only HTML projects.

A nested override

[$raw: <div>Raw HTML here</div>]

Nested templates default to $md, but an explicit directive overrides that.

Compile-time only

Everything in a .mtf body folds during compilation. A runtime value cannot be used there. The file produces one content string and makes no page fragment of its own.

A .mtf file has a small compile-time scope. It sees the fixed template prelude and can select declarations from same-module helper files with qualified references. It has no implicit access to module root declarations.

Same-module helpers

An ordinary .moth helper declaration is reachable from a .mtf file in the same module through a qualified reference:

-- docs/settings.moth
site_name #= "Moth"
-- docs/intro.mtf
# Welcome to [@settings site_name]

The provider path names the helper file, the selected symbol names one compile-time declaration and the two tokens stay on the same line. The template folds the selected value during compilation. The directory needs no @mod.moth file for this to work.

A qualified reference introduces no local binding. It therefore never shadows a prelude name or another declaration. Selecting @home_constants p produces that helper's p, even though the prelude also exposes p.

Root-local declarations stay hidden

Declarations authored directly in @mod.moth, @package.moth or @entry.moth belong to that root file only. A .mtf file cannot read them, even when the facade exports the declaration:

-- docs/@mod.moth
$export
site_name #= "Moth"
-- docs/intro.mtf
# Welcome to [site_name]

The unqualified name fails with a root-local visibility diagnostic instead of resolving through the facade. Naming the root file directly also fails, because root files are not qualified-reference providers.

Move the shared value into an ordinary helper file when a template needs it. A helper declaration is shared by every root and selectable from the template.

The fixed template prelude

Exported compile-time constants from @core/html and @core/css are visible flat inside every .mtf body. This includes HTML helpers such as p and codeblock, and style fragments such as center.

-- docs/intro.mtf
[p: This paragraph uses the Core HTML p helper.]

[h1, center: This heading uses the Core CSS center fragment.]

[codeblock, $code "moth": value = 42]

The prelude is fixed compiler policy under every builder, including custom SDK builders. It reads the official packages/core/html and packages/core/css Moth source packages, which are registered once as Core/MothSource roots. Each package publishes through its directly contained @package.moth. A builder cannot replace either package or change .mtf availability. Both packages precede every .mtf consumer, so a failed prelude package blocks the consumer.

The prelude supplies compile-time constants only. It is not a general template-wide import mechanism, and it never includes runtime values, types, private constants, project-local packages or arbitrary builder-registered constants. A public constant declared anywhere inside a provider package enters it under its exported name, including when another file in that package exports it with as. A re-export or alias of a declaration from another package does not.

Collision semantics

A real local binding that matches a prelude name fails with an ordinary visible-name collision. The diagnostic identifies both sources where location data is available.

A qualified selection creates no local binding, so it cannot collide merely because the selected name matches a prelude constant:

-- docs/home_constants.moth
p #= "local p"
-- docs/intro.mtf
[p][@home_constants p]

The first p comes from the prelude and the second from the helper. Both render.

Nested templates

Nested templates inside a .mtf body are valid. A nested template with no explicit directive defaults to $md. Any explicit directive (such as $raw, $fresh, $html, $code, $css or $escape_html) overrides the moth template Markdown default for that nested template.

[$raw: <div>Raw HTML output</div>]

Constants versus runtime values

Qualified references inside a .mtf file must resolve to compile-time constants. Runtime bindings, runtime functions, types, traits, methods, external runtime APIs and the generated self content constant are not valid template values.

A reference to a runtime value is rejected with MOTH-RULE-0034 or MOTH-RULE-0053.

No runtime capture

Moth template bodies are const-required. There is no runtime capture. The entire body must fold at compile time.

Allowed template directives

Formatting and child-selection directives available in the compile-time template environment are valid inside .mtf. This includes $md, $raw, $fresh, $html, $code, $css and $escape_html.

A .mtf file produces one fixed content constant. It cannot expose its own slot receiver or contribute a builder root slot.

Child-template formatting boundary

Child templates are opaque barriers to the parent formatter. A child template's output is not inspected by the parent $md formatter. Template delimiters cannot pair across child boundaries.

Literal delimiter output

To output literal template delimiters, use string insertion:

["["]
["]"]
["[literal]"]

Content helpers

Content helpers such as codeblock come from the fixed @core/html prelude above, and style fragments come from @core/css. Neither comes from the active builder. A .mtf file can also name an ordinary helper with an explicit qualified reference. It does not gain that helper's declarations automatically.

Concepts related to template scope

Choose the explanation level: Moth template file limits

Moth template file limits

Moth template files are the wrong tool when the job stops being content-shaped.

Use .moth instead when

  • You need runtime values or control flow.
  • You need dependency clauses or declarations.
  • You need page fragments or slot composition.

Use .md instead when

  • You need plain Markdown with no Moth syntax.
  • You need fenced code blocks or pipe tables.
  • You need raw HTML preserved.

Moth template files are intentionally small. These are the confirmed restrictions and deferred surfaces.

Confirmed restrictions

  • Implicit const-required evaluation: the entire .mtf body must fold at compile time. There is no runtime path.
  • No normal top-level declarations: a .mtf file does not contain authored dependency clauses, functions, structs, choices, type aliases or trait declarations. Text that looks like a declaration is template body content, not a compiled declaration.
  • No runtime bindings or calls: runtime values are not visible. A reference to a runtime name is rejected with MOTH-RULE-0034 or MOTH-RULE-0053.
  • Fixed content only: a .mtf file cannot expose a template slot receiver or contribute a builder root slot. Its generated content constant must fold.
  • No fenced code blocks: triple-backtick blocks are literal text in $md, not <pre><code>. Use codeblock helpers instead.
  • No pipe tables: Markdown pipe table syntax is literal text in $md, not <table>. Use structured lists or page-owned table components.
  • No raw backtick strings: backticks are Markdown inline-code delimiters, not raw string syntax.
  • Template and Markdown child boundaries: child templates are opaque barriers to the parent formatter. Delimiters cannot pair across child boundaries.
  • Const-required template control flow: template if includes one body for a true Bool or present option, and template loop repeats its body. Both are compile-time-only in .mtf. The selector and body of a conditional must be compile-time evaluable even when the body is inactive. Template if has no else or else if form, and template loops have no break or continue controls. Runtime statement control flow and runtime function calls are unavailable.

Differences from normal .moth

  • A .moth file can contain dependency clauses, declarations, runtime code and page fragments. A .mtf file cannot.
  • A .moth file has normal Moth source scope. A .mtf file has a restricted flat compile-time scope.
  • A .moth file can produce page fragments. A .mtf file produces one content #String constant.

Differences from plain .md

  • A .mtf file is parsed as Moth template syntax. A .md file is not.
  • A .mtf file can access compile-time constants and use nested templates. A .md file cannot.
  • A .mtf file uses the $md renderer. A .md file uses the plain Markdown renderer (CommonMark-compatible with GFM extensions).
  • A .mtf file escapes raw HTML. A .md file preserves raw HTML.

When to choose normal .moth code

Use normal .moth code when:

  • runtime values or richer decisions are needed
  • full slot and wrapper composition is needed
  • the content is part of broader program logic
  • dependency clauses or declarations are needed in the same file

Concepts related to Moth template limits