Template API Reference
Every .marko file exposes its runtime API on its default export. Targeted compilation emits render in server builds and mount in browser builds.
Template.render(input)
| Parameter | Default | Details |
|---|---|---|
input | {} | Template input object; may also include $global for render-scoped globals |
Available in server builds. Returns a render result that can be consumed as a stream, async iterable, ReadableStream, promise, or synchronous string.
A render result can only be consumed once; later consumers fail (thrown, rejected, or emitted as a stream error). Awaiting the same result multiple times is the sole exception because the first await caches its promise.
Async Iterator
The render result implements the async iterable protocol for for await...of, yielding HTML string chunks:
import Template from "./template.marko";
for await (const chunk of Template.render({})) {
// write the HTML chunk
}Each iteration yields HTML flushed since the previous pull; synchronous renders yield a single chunk. Exiting the loop early (break, return, or throw) aborts any pending render, and calling iterator.throw(reason) aborts with reason.
Pipe
.pipe(target) streams rendered HTML into a Node.js stream.Writable (or any object implementing write(chunk) and end()). If target.flush() exists, it is called after each chunk so buffering transforms such as zlib.createGzip() flush incrementally:
import http from "node:http";
import Template from "./template.marko";
http
.createServer((req, res) => {
Template.render({}).pipe(res);
})
.listen(3000);ReadableStream
.toReadable() returns a WHATWG ReadableStream<Uint8Array> yielding UTF-8 encoded chunks, suitable for Web APIs such as Response and edge runtimes:
const response = new Response(Template.render({}).toReadable(), {
headers: { "content-type": "text/html" },
});Consumption starts on the stream's first pull, so an unread Response leaves the render unconsumed. Cancelling the stream aborts the render with the cancellation reason.
Thenable
The render result is a thenable whose resolved value is the full buffered HTML string, supporting await, .then(), .catch(), and .finally():
const html = await Template.render({});Awaiting buffers the full HTML document into a single string rather than streaming chunks.
toString
.toString() returns the rendered HTML synchronously when the template has no asynchronous content:
const html = Template.render({}).toString();Any asynchronous rendering (such as <await>) or an already aborted $global.signal causes .toString() to throw rather than return partial HTML.
Template.mount(input, node, position?)
| Parameter | Default | Details |
|---|---|---|
input | {} | Template input object; may also include $global |
node | (required) | Light DOM Element where the template is inserted |
position | "beforeend" | Insertion point relative to node, matching Element.insertAdjacentHTML |
Available in browser builds. Builds the reactive DOM and inserts it relative to node:
Template.mount({}, document.body); // append inside body
Template.mount({}, document.body, "afterbegin"); // prepend inside bodynode must be a light DOM Element (not a ShadowRoot or DocumentFragment): Marko inherits the element's HTML namespace during parsing and delegates events on document.
Supported position values match insertAdjacentHTML():
<!-- "beforebegin" -->
<p>
<!-- "afterbegin" -->
existing body content
<!-- "beforeend" (default) -->
</p>
<!-- "afterend" -->Mounted Instance
Template.mount() returns a mounted instance with methods to update or destroy the template and read its return value:
const instance = Template.mount({ name: "foo" }, document.body);Use instance.update() and instance.destroy() only at client-only mount boundaries or in tests. Within Marko templates, manage updates through the reactive system.
instance.update(input)
Applies new input synchronously as a reactive update. $global is fixed at mount time and ignored on updates:
instance.update({ name: "bar" });instance.destroy()
Removes the template's DOM nodes, aborts every $signal, and runs cleanup hooks:
instance.destroy();instance.value
Reflects the tag variable exposed by the template's <return>:
<let/color="#ff8000">
<input type="color" value:=color>
<return=color valueChange(newColor) { color = newColor }>let/color="#ff8000"
input type="color" value:=color
return=color valueChange(newColor) { color = newColor }import ColorPicker from "./color-picker.marko";
const instance = ColorPicker.mount({}, document.body);
instance.value; // "#ff8000"
instance.value = "#0080ff"; // invokes `valueChange` when return is assignableinput.$global
A $global property passed in input to render or mount is stripped from input and exposed as $global across every template in the render tree. Application-specific globals sit alongside Marko's built-in properties and are typed on Marko.Global.
$global.serializedGlobals
string[] | Record<string, boolean> | undefined
$global stays server-only by default. Properties listed in serializedGlobals are also serialized into the HTML so browser code can read them on $global:
Template.render({
$global: {
locale: "en-GB",
apiToken: "secret",
serializedGlobals: ["locale"],
},
});An object form suits lists assembled across middleware, matching Marko Run's ctx.serializedGlobals:
serializedGlobals: { locale: true, apiToken: false }Properties holding undefined are omitted.
Serialized values are embedded in the HTML response and visible to clients. Keep secrets off serializedGlobals.
$global.signal
AbortSignal | undefined
Aborts pending async rendering and cleans up active work when signaled, such as when an incoming HTTP request disconnects.
$global.cspNonce
string | undefined
Adds a nonce attribute to Marko-rendered <script> and <style> elements: <html-script> and <html-style>, dynamic <style> elements, and inline streaming/resume scripts. An explicit nonce attribute (or spread) on an element takes precedence.
Marko automatically serializes cspNonce to the client whenever it is set on $global, so it does not need to be listed in serializedGlobals.
const cspNonce = crypto.randomUUID();
res.setHeader(
"Content-Security-Policy",
`script-src 'nonce-${cspNonce}'; style-src 'nonce-${cspNonce}'`,
);
Template.render({
$global: { cspNonce },
}).pipe(res);$global.renderId
string | undefined
Isolates multiple independent renders sharing one runtime in the same document. Defaults to "_", except for non-page templates (whose rendered tag tree contains no html, head, or body element) compiled with linkAssets, which receive a fresh random ID per render() call. mount() always defaults to "_".
Set an explicit renderId when multiple renders of page templates share a document:
Template.render({
$global: { renderId: "cart" },
});renderId and runtimeId become JavaScript identifiers in inline resume scripts and must match /^[A-Za-z_][A-Za-z0-9_]*$/ (no UUIDs or hyphens).
$global.runtimeId
string | undefined
Names the global variable holding resume data for a Marko runtime instance (default "M"), isolating multiple Marko runtimes on the same page. Follows the same identifier rule as renderId.
Server and browser builds must match, so configure runtimeId in the bundler (for example @marko/vite's runtimeId option) rather than per render call.
Contributors
Helpful? You can thank these awesome people! You can also edit this doc if you see any issues or want to improve it.