Skip to content

Search tags, filters, functions and guides.

Tags / export

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 to undefined when the template has no export with that name.
  • It takes the same state, locale, timeZone and signal options as render, so functions wrapped in withRender work.
  • kiln.analyze(name) lists a template's exports in exports without running anything. Each has a name, and a value when the export is a literal, as in {% export revalidate = 300 %}. literal says 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 a misplaced-export error.
  • A template can export each name once. Exporting it again is a duplicate-export error.
  • 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.