Templates
Template context (stati)
Every Eta template receives a TemplateContext object with site metadata, front matter, and rendered content:
interface TemplateContext {
site: SiteConfig;
page: {
path: string;
url: string;
content: string;
toc: TocEntry[]; // h2 to h6 headings; empty when there are none or markdown.toc is false
navNode?: NavNode; // Current page's navigation node
wordCount?: number; // Words in the Markdown body
readingTime?: number; // Whole minutes to read, at least 1
[key: string]: unknown; // Front matter fields
};
content: string; // Rendered HTML content
body?: string; // The HTML this template wraps (see stati.body and stati.props)
props: Record<string, unknown> | undefined; // Props of the stati.include() or stati.layout() call
include: TemplateIncludeFunction; // Renders another template and returns its HTML
layout: TemplateLayoutFunction; // Wraps this template's output in another template
helpers: Record<string, (...args: any[]) => unknown>; // The functions in helpers in the config
nav: {
tree: NavNode[];
getTree: () => NavNode[];
findNode: (path: string) => NavNode | undefined;
getChildren: (path: string) => NavNode[];
getParent: (path?: string) => NavNode | undefined;
getSiblings: (path?: string, includeSelf?: boolean) => NavNode[];
getSubtree: (path: string) => NavNode[];
getBreadcrumbs: (path?: string) => NavNode[];
getCurrentNode: () => NavNode | undefined;
};
collection?: CollectionData; // The page's collection; undefined when it has none
pagination?: PaginationData; // This page's part of a listing with `paginate`
generated?: GeneratedPageData; // Key and items of a page from a [param].md template
slugify: (text: string) => string; // The slug Stati gives generated page keys
url: (path: string) => string; // A site path with the base path of site.baseUrl added
assets?: StatiAssets; // Bundle, stylesheet and search index paths
propValue: (...args: PropValueArg[]) => string; // Class/attribute value builder
generateSEO: (tags?: string[]) => string; // SEO tag generator
image: (src: string, options?: ImageOptions) => string; // Sized, lazy-loaded <img> or <picture>
ogImage?: GeneratedOgImage; // The page's generated Open Graph image, with images.og
og?: { width: number; height: number }; // Card size, only while og.eta renders
generator: { version: string }; // Stati core version
}
interface TemplateIncludeFunction {
(name: string, props?: Record<string, unknown> | null, body?: string | null): string;
has(name: string): boolean; // Whether name resolves to a template for this page
}
interface TemplateLayoutFunction {
(name: string, props?: Record<string, unknown> | null): string; // Returns ''
has(name: string): boolean; // The same function as stati.include.has
}
interface TocEntry {
/** Anchor ID for the heading (used in href="#id") */
id: string;
/** Plain text content of the heading */
text: string;
/** Heading level (2-6) */
level: number;
}
interface SiteConfig {
title: string;
baseUrl: string;
defaultLocale?: string;
description?: string;
}
interface ImageOptions {
alt?: string;
sizes?: string;
priority?: boolean;
class?: string;
title?: string;
}
interface GeneratedOgImage {
url: string; // Absolute
path: string; // Root-relative
width: number;
height: number;
alt?: string; // The page title
}
interface StatiAssets {
/** Array of TypeScript bundle paths matched for this page */
bundlePaths: string[];
/** Paths of the stylesheet Stati builds with Tailwind (empty without --tailwind-input) */
stylesheetPaths: string[];
/** Path to search index file (when search enabled) */
searchIndexPath?: string;
}
interface PaginationData {
pageNumber: number; // Starts at 1
totalPages: number; // At least 1
items: PageModel[]; // This page's share of the listing, newest first
prevUrl?: string; // Absent on the first page
nextUrl?: string; // Absent on the last page
urls: string[]; // Every page's URL; urls[0] is the first page
}
interface GeneratedPageData {
key: string; // In the URL: 'css'
label: string; // As written: 'CSS'
items: PageModel[]; // Pages with this key, newest first
template: string; // 'blog/tags/[tag].md'
data?: Record<string, unknown>; // From a collections.generate entry
}
stati.collection is the page’s own collection on a folder’s index page, the collection it belongs to on other pages, or the one its collection front matter names. stati.pagination and stati.generated are set on the pages they describe and undefined elsewhere. stati.collection.related(url, options?) lists the pages most related to one of its pages (see Related pages). Reading stati.collection or stati.nav, or calling related(), makes the page depend on that data for ISG. See Collections and generated pages.
The root of stati holds only Stati’s own API. Your functions live under stati.helpers, so a helper named page or slugify never replaces Stati’s value.
Front matter values are exposed through stati.page. Additional properties you place on site in a JavaScript config are available at runtime as stati.site.yourKey, but the SiteConfig type only declares the four fields above, so a TypeScript config rejects them.
During stati dev, reading a stati.* property that does not exist (for example a misspelled helper, or stati.site.author when your config has no site.author) throws an error instead of returning undefined, suggesting the property you probably meant or listing the available ones: stati.helpers.formatDat and stati.formatDate both suggest stati.helpers.formatDate. Front matter under stati.page is not checked. Test for optional values with the in operator: 'author' in stati.site. See Unknown template properties.
The nav object provides helper methods for working with navigation. See Navigation API for details on getTree(), findNode(), getBreadcrumbs(), and other navigation helpers.
page.toc lists the page’s h2 to h6 headings in document order. See Table of contents.
page.wordCount and page.readingTime come from Stati. They are not front matter, so a front matter key of the same name is replaced, with a warning. The pages in stati.collection, stati.pagination.items and stati.generated.items carry the same two properties, as post.wordCount and post.readingTime. See Word count and reading time.
Includes and layouts
stati.include(name, props?, body?)
Renders the template name and returns its HTML. Print the result with <%~ %>:
<%~ stati.include('partials/header') %>
<%~ stati.include('partials/card', { title: 'Hello', featured: true }) %>
<%~ stati.include('partials/callout', { tone: 'note' }, '<p>Inside the callout</p>') %>
| Argument | Description |
|---|---|
name |
The template’s path without .eta. partials/card is looked up from the page’s folder up to site/, and the nearest match wins; /partials/card is read from site/ only; ./card and ../shared/card are relative to the template making the call. A .eta suffix is dropped. |
props |
An object the template reads as stati.props |
body |
HTML the template prints as stati.body |
- Included templates render synchronously, so they cannot use
await. - Without props and body, a template renders once per page; later includes of it on the same page reuse that HTML.
- The included template can call
stati.layout()to wrap its own output in a frame. - A name never resolves outside
site/. An unknown name opens the error overlay in development, listing similar templates; in production the build logs the error and the include returns''. - An error thrown inside the included template opens the overlay at that file in development. In production the build logs it, and an HTML comment takes the template’s place.
stati.layout(name, props?)
Wraps the output of the calling template in the template name, once the calling template has rendered. The call returns '', so it can sit in <% %> or <%~ %>, anywhere in the template:
<% stati.layout('base', { bodyClass: 'post' }) %>
<article><%~ stati.content %></article>
- The named template prints the wrapped output as
stati.bodyand reads the props asstati.props. It can callstati.layout()in turn. - Names resolve the same way as
stati.include()names. - One call per template: a second call is an error in development; in production the build warns and the last call wins.
- An unknown name is an error in development; in production the build logs it and the template renders without the wrapper.
stati.include.has(name) and stati.layout.has(name)
Whether name resolves to a template for the current page, with the lookup the call would use. The two are the same function:
<% if (stati.include.has('partials/sidebar')) { %>
<%~ stati.include('partials/sidebar') %>
<% } %>
<% stati.layout(stati.layout.has('blog/base') ? 'blog/base' : 'base') %>
stati.body and stati.props
| Template | stati.body |
stati.props |
|---|---|---|
| The page’s layout (front matter or discovery) | Absent; the page is stati.content |
undefined |
A template named by stati.layout() |
The output it wraps | The call’s props |
A template rendered by stati.include() |
The body argument, or undefined |
The call’s props, or undefined |
The Open Graph template (og.eta) |
Absent | undefined |
stati.body is HTML, so print it with <%~ %>. Reading it where it is absent is an error in development that points at stati.content. A body that is not a string, and props that are not an object, are errors in development; in production the build warns and renders without them.
<!-- Fallback pattern: props → page → site title -->
<%= stati.props?.author || stati.page.author || stati.site.title %>
Eta’s include, layout and block
Eta’s own include(), includeAsync(), layout(), block() and blockAsync() are not available in Stati templates:
| Call | Development | Production |
|---|---|---|
include('x', data) |
Error: “Use stati.include('./x', data) instead” |
Error logged; renders '' |
includeAsync('x') |
The same, noting that includes render synchronously | Error logged; renders '' |
layout('x', data) |
Error: “Use stati.layout('./x', data) instead; the data arrives as stati.props” |
Error logged; no layout applied |
block(), blockAsync() |
Error: pass the content to the layout as a prop | Error logged; renders '' |
The suggested name keeps Eta’s resolution: a name that does not start with /, ./ or ../ gets ./ in front, so it stays relative to the calling template.
Built-in functions
stati.propValue(...args)
Builds property values from various inputs, similar to the classnames library. Accepts strings, arrays, and objects, filtering out falsy values. Especially useful for building dynamic HTML attributes that support space-separated tokens (like class or data-analytics).
Signature:
type PropValueArg =
| string
| number
| boolean
| null
| undefined
| Record<string, boolean | string | number | null | undefined>
| PropValueArg[];
stati.propValue(...args: PropValueArg[]): string
Examples:
<!-- Basic usage -->
<button class="<%= stati.propValue('btn', `btn-${variant}`, isActive && 'is-active') %>">
Click me
</button>
<!-- Array syntax -->
<div class="<%= stati.propValue(['card', 'card-hover', featured && 'featured']) %>">
Card content
</div>
<!-- Object syntax (keys with truthy values are included) -->
<article class="<%= stati.propValue({
'post': true,
'post-featured': stati.page.featured,
'post-draft': stati.page.draft,
[`post-${stati.page.category}`]: stati.page.category
}) %>">
Article content
</article>
<!-- Data attributes -->
<div
class="<%= stati.propValue('card', `hover:border-${color}-300`) %>"
data-analytics="<%= stati.propValue('card-click', `category-${stati.page.category}`) %>"
>
Card with analytics
</div>
propValue() also tracks Tailwind classes for inventory when Tailwind integration is enabled, ensuring dynamically-generated classes are included in the CSS build.
See Dynamic attribute values for the attribute convention.
stati.url(path)
Adds the base path of site.baseUrl to a path from the site root, so a link works when the site is served from a subfolder. With baseUrl: 'https://example.com/docs', stati.url('/posts/hello') is /docs/posts/hello. Without a path in baseUrl it returns the path as it is, so templates that use it build the same site as before.
<a href="<%= stati.url('/') %>">Home</a>
<a href="<%= stati.url(post.url) %>"><%= post.frontMatter.title %></a>
<link rel="icon" href="<%= stati.url('/favicon.svg') %>">
- It leaves absolute URLs (
https://…,mailto:), protocol-relative URLs (//cdn.example.com/a.js),#hashand?queryvalues and relative paths (./x) as they are. - A path that already starts with the base path is returned as it is, so calling it twice is safe. This also means that a path of your site that begins like the base path, such as
/docs/introon a site served from/docs, is not prefixed again; avoid a section named like the base path, or write the link in Markdown, which always adds the prefix. page.url, navigationurls, collection entries, pagination URLs,stati.assets.*paths and front matter stay relative to the site root. Pass them throughstati.url()when you print them in a link.- Stati adds the prefix itself to what it writes: bundle scripts, the Tailwind stylesheet link it adds, the search index meta tag,
stati.image()and Markdown image URLs, and root-absolute links in Markdown.
stati.image(src, options?)
Renders an image from a layout or an included template the way Stati renders Markdown images: with its width and height, loading="lazy" and decoding="async", and as a <picture> with AVIF and WebP versions when images.responsive is on. It returns HTML, so print it with <%~ %>:
<%~ stati.image(stati.page.cover, { alt: stati.page.coverAlt, priority: true }) %>
| Option | Description |
|---|---|
alt |
Alternative text. Use '' for a decorative image; Stati warns when alt is missing. |
priority |
Load eagerly with fetchpriority="high", for the main image above the fold. |
sizes |
The sizes attribute for responsive images. |
class, title |
Added to the <img>. |
- A path that starts with
/is read frompublic/; any other path is read next to the page’s Markdown file and copied to/_img/. - An empty
srcreturns an empty string, so an optional cover image needs no guard. - The function is synchronous, so it works anywhere in a template, included templates too.
- With
images: false, it returns a plain<img>withsrc,alt,titleandclass.
stati.ogImage
With images.og, the page’s generated card: url (absolute), path, width, height and alt. It is undefined when the page has no generated card, for example because its front matter sets its own image. Stati’s SEO injection and stati.generateSEO() already write the tags, so you only need this in a layout that writes its own Open Graph tags:
<% if (stati.ogImage) { %>
<meta property="og:image" content="<%= stati.ogImage.url %>">
<% } %>
In an og.eta card template, stati.og holds the card’s width and height.
Helpers
Register your own functions under helpers in the configuration, and call them from any template as stati.helpers.name():
<time><%= stati.helpers.formatDate(stati.page.date) %></time>
<h1><%= stati.helpers.uppercase(stati.page.title) %></h1>
helpers is typed Record<string, (...args: any[]) => unknown>, so a helper declares whatever parameters it needs. Helpers reach every template Stati renders: layouts, the shells they name, included templates and the Open Graph template. A helper written with function or method syntax gets the helpers object as this.
Related documentation
- Templates and layouts - Layouts, shells and includes, with examples
- TypeScript Types - Full type definitions for template context
- Build Hooks - Customize rendering with lifecycle hooks
- Template Configuration - Configure helpers and template options
- Navigation API - Working with navigation data in templates