Tags
export
Give your code a value from a template without rendering it.
export gives your code a value from a template without rendering it, such as the pages a template builds or how long a server may cache it. Only your code reads exports. The template itself never sees them.
{% export paths = documents("post")|map(post => post.url) %}
{% export revalidate = 300 %}
{% set post = document("post", route.slug) %}
<h1>{{ post.title }}</h1>
const paths = await kiln.evaluate("pages/post", "paths");
// ["/blog/hello", "/blog/world"]
Use exports for what a host needs to know about a template before, or instead of, rendering it: the paths a static build should render, a page's title for a menu, or how long to cache it. Your code decides what each export means. Kiln only works out its value.
Reading exports
kiln.evaluate(name, exportName, options?)works out one export and resolves to its value, waiting for async functions. It resolves toundefinedwhen the template has no export with that name.- It takes the same
state,locale,timeZoneandsignaloptions asrender, so functions wrapped inwithRenderwork. kiln.analyze(name)lists a template's exports inexportswithout running anything. Each has aname, and avaluewhen the export is a literal, as in{% export revalidate = 300 %}.literalsays which.
Scope
An export runs on its own, before there is a page to render, so it can only use globals, functions, filters, tests and its own arrow function parameters. Reading one of the template's variables, such as a prop or a variable it sets, is an export-scope error.
{% set posts = documents("post") %}
{# export-scope: "posts" is a variable of the template #}
{% export paths = posts|map(post => post.url) %}
Rules
- Exports go at the top level of a template, including one that uses
extends. Anywhere else is amisplaced-exporterror. - A template can export each name once. Exporting it again is a
duplicate-exporterror. - An export never runs when its template renders, and outputs nothing, so it costs nothing per render.
- Only a template's own exports count. The exports of its layout, includes and components are separate.