Skip to content

Search tags, filters, functions and guides.

JavaScript API / Rendering

JavaScript API

Rendering

Render pages, single blocks and request data.

render turns a template into a string. Pass the template's name and the variables it should see.

const html = await kiln.render("pages/post", {
	route: { path: "/blog/hello/" },
	post,
});
  • render always returns a promise, but only waits when a template calls something async.
  • If anything goes wrong, the promise rejects and no partial output is returned. See Errors and checks.
  • Generate TypeScript types to have your editor check template names and data.
  • To send the page while it renders, use renderStream.

Render options

The third argument sets options for one render:

OptionDescription
stateData for this request, passed to functions made with withRender. See Request data
globalsGlobals for this render, layered over the environment's. See Globals for one render
localeOverrides the environment's locale
timeZoneOverrides the environment's time zone
signalAn AbortSignal. Kiln stops the render as soon as it is aborted, even while it waits for your code
islandsDefers island tags with deferIslands(), or streams them with streamIslands() and renderStream. Without it, islands render in place
const html = await kiln.render("pages/home", context, {
	locale: "fr-FR",
	timeZone: "Europe/Paris",
	signal: request.signal,
});

Globals for one render

globals sets variables that every template in the render can read, as globals do: the page, its layouts, included templates, components, macros and islands. Use it for a value that is the same for the whole render but changes between renders, such as the locale of the content.

const kiln = createKiln({
	loader,
	globals: { site: { name: "Acme" }, contentLocale: "en" },
});

const html = await kiln.render("pages/home", context, {
	globals: { contentLocale: "fr" },
});
  • A render's globals win over the environment's globals of the same name. The environment's other globals stay available.
  • check, analyze, generated types, exports and your editor only know the environment's globals. Give each per-render global a default there, as contentLocale has above. Otherwise they treat it as render data, and exports can't read it.
  • Templates can't assign to them, as with any global.
  • cache keys don't include them. Add any that change a cached section's output to its key.

Rendering one block

renderBlock renders a single block of a template. Use it to send a fragment of a page, for example in reply to an htmx or fetch request.

const results = await kiln.renderBlock("pages/search", "results", { query });
  • Kiln picks the block the full page would use, so a page's version wins over its layout's.
  • Top-level set tags in the page still run, so data the page loads is available.
  • The block doesn't see variables the layout creates around it, such as a for loop's variable.
  • Asking for a block that doesn't exist is an error.

Caching

Kiln compiles each template the first time it is used, then keeps the compiled version. The loader is called once per template name. If loading or compiling fails, Kiln tries again on the next render.

Kiln also remembers the last thousand or so names the loader didn't find, so a missing template isn't asked for on every render.

When a template changes or is added, for example after an edit in your CMS, call invalidate:

kiln.invalidate("pages/home"); // one template
kiln.invalidate(); // all templates