Templates

Templates are MiniJinja, with Jinja2 syntax. They see the page as this, the site as site, the config as config, and data files through bag().

Templates live in templates/ and use MiniJinja, a Rust implementation of Jinja2. Everything in the MiniJinja syntax documentation works: {% extends %}, {% block %}, {% include %}, {% macro %}, {% for %} with {% else %}, {% set %}, and the built-in filters and tests such as join, length, default, selectattr, first and is defined.

A site with a theme also has the theme's templates, and a template of the site replaces the theme's of the same name.

Which template a page uses

Every page that is built is rendered to <URL>index.html: the page /about becomes about/index.html. Its template is, in order:

  1. the page's _template, if set;
  2. the model's [model] template, if set;
  3. <model id>.html, such as blog-post.html.

A missing template is motabhai::template::not_found, reported on the page. Templates can be in subdirectories and are named by their path in templates/, as in {% include "partials/footer.html" %}.

{# templates/layout.html #}
<!doctype html>
<html lang="{{ config.language }}">
<head>
  <title>{% block title %}{{ this.title }}{% endblock %} · {{ config.name }}</title>
  <link rel="stylesheet" href="{{ '/css/main.css'|asseturl }}">
</head>
<body>
  {% block body %}{% endblock %}
</body>
</html>
{# templates/page.html #}
{% extends "layout.html" %}
{% block body %}
<h1>{{ this.title }}</h1>
{{ this.body }}
{% endblock %}

Other templates have special roles: flow block templates, which default to blocks/<block id>.html (Flow blocks); the tag page template, tag.html by default; and 404.html (Listings, feeds and URLs).

Escaping and undefined values

Output of templates whose names end in .html, .htm or .xml is HTML-escaped: Fish & <Chips> prints as Fish &amp; &lt;Chips&gt;. Values that are already HTML are not escaped: markdown and html fields, flow fields and the markdown filter's output. A template with another extension, such as page.txt, is not escaped at all.

Using anything undefined is an error rather than an empty string: {{ this.titel }} for a field the model does not declare is motabhai::template::undefined, with the template's line and column. A declared field that the page leaves out is none, which prints as nothing, so {% if this.summary %} is the way to test for one. Use is defined to test a name without failing.

A syntax error is motabhai::template::syntax, and any other failure while rendering is motabhai::template::render. Both point into the template. Template errors are reported once per template, however many pages use it, and errors in different templates are reported together.

Globals

NameWhat it is
thisThe page being rendered.
siteThe whole site: site.root, site.get() and site.query().
configValues from motabhai.toml.
bag(name, ...)A function reading the TOML files in databags/.
tagOn tag pages only: the tag being shown.
recordIn flow block templates only: the page the block belongs to (this is the block).

this: pages

A page reads its fields as attributes: this.title, this.pub_date. It also has:

AttributeValue
any fieldThe field's value; see the type table in Models and field types.
_id, _path, _alt, _model, _template, _slug, _hidden, _discoverable, _createdThe system fields. _created is none for a page that does not set it.
format"markdown" or "notebook": the kind of content file the page was read from.
url_pathThe page's URL path without the base path, such as /blog/first-post/. Use the url filter for links.
parentThe parent page; none for the root.
childrenA query of the children; see below.
prev, nextThe previous and next sibling in the parent's default order, or none. Pages left out of that listing have neither.
attachmentsThe page's attachments, in name order.
alternativesThe page in each language, for a language switcher; see Translations.
paginationOn paginated pages only; see Listings, feeds and URLs.
tag_pagesThe tag pages of a model with [tags].
tag_page(tag)One tag page, as in this.parent.tag_page("rust").

pagination and tag_pages are undefined on a page whose model has no [pagination] or [tags] table.

A page prints as its content path, and two pages compare equal when they are the same page, so {% if page == this %} marks the current page in a menu.

<nav>
  {% if this.next %}<a href="{{ this.next|url }}">← {{ this.next.title }}</a>{% endif %}
  {% if this.prev %}<a href="{{ this.prev|url }}">{{ this.prev.title }} →</a>{% endif %}
</nav>

Attachments

Each item of this.attachments has:

AttributeValue
idThe file name, such as hero.jpg.
pathThe content path, such as /blog/first-post/hero.jpg.
url_pathThe URL path without the base path.
recordThe page it belongs to.
width, height, format, exifFor images; none otherwise.
thumbnail(width, height, mode)A resized copy of an image.

An attachment prints as its content path, and url links to it. Images are described in Assets and images.

site

UseResult
site.rootThe root page.
site.get("/blog/first-post")The page at that content path, or none. Hidden pages are found too. A trailing / or missing leading / is fine.
site.get("/about", "de")The page in another language.
site.query("/blog")A query of that page's children, like site.get("/blog").children.

All of them work in the language of the page being rendered. A language the site does not have, or anything but a content path, is motabhai::template::invalid_query.

config

AttributeValue
config.nameproject.name.
config.urlproject.url, always ending in /, or none.
config.languageThe primary language, project.language.
config.base_pathThe base path, such as /repo/, or /.
config.alternativesEvery language, primary first, each with code, name, url_prefix and primary.

bag(): databags

A databag is a TOML file in databags/. bag("menu") returns all of databags/menu.toml:

# databags/menu.toml
[[items]]
label = "Home"
path = "/"

[[items]]
label = "Blog"
path = "/blog"
{% for item in bag("menu").items %}
<a href="{{ item.path|url }}">{{ item.label }}</a>
{% endfor %}

A dotted name, or more arguments, looks inside the file. Numbers index lists, and a key that is not there is none rather than an error:

{{ bag("menu.items.0.label") }}       {# Home #}
{{ bag("menu", "items", 1, "path") }}  {# /blog #}

Dates in a databag are text in ISO 8601, which dateformat accepts.

An unknown databag is motabhai::template::unknown_databag, and a databag that is not valid TOML fails the build (motabhai::toml) even if no page reads it. A page that reads any part of a databag is rebuilt when the file changes. Databags are the site's own: a theme has none.

Filters

FilterWhat it does
urlMakes a link that works under the base path.
asseturlLinks to a file in assets/, by its fingerprinted name if it has one.
markdownRenders a string as Markdown, resolving links against the page being rendered.
dateformat(format)Formats a date or date-time with a strftime format. The default format is %Y-%m-%d.

url

url takes a page, an attachment, a thumbnail, a tag page, a page of a paginated list, an entry of this.alternatives, or a string, and returns a link under the base path. On a site whose url is https://user.github.io/repo/, rendering /about (served at /about-us/):

ExpressionResult
this|url/repo/about-us/
site.root|url/repo/
"/about"|url/repo/about-us/: a content path links to the page
"/about?x=1#team"|url/repo/about-us/?x=1#team
"/blog/feed.xml"|url/repo/blog/feed.xml
"photo.jpg"|url/repo/about-us/photo.jpg: relative to the page
"../"|url/repo/
site.get("/notes and ideas")|url/repo/notes%20and%20ideas/: names are percent-encoded
"https://example.com/x"|urlunchanged, as are //host/..., #top and mailto: links

Anything else, such as a number, is motabhai::template::invalid_url.

asseturl

<link rel="stylesheet" href="{{ '/css/main.css'|asseturl }}">
{# /repo/css/main.3f2a9c1b.css #}

The path is relative to assets/, with or without a leading /. With a theme, it is the site's file if there is one, else the theme's. A file that does not exist, or a stylesheet partial whose name starts with _, is motabhai::template::missing_asset.

markdown

{{ this.summary|markdown }}

The result is HTML, so it is not escaped. See Markdown below.

dateformat

Dates print as ISO 8601 text, such as 2026-11-29 or 2026-11-29T08:30:00+01:00. dateformat formats them, and also accepts ISO 8601 strings:

{{ this.pub_date|dateformat("%d %B %Y") }}   {# 29 November 2026 #}
{{ this.pub_date|dateformat("%B %-d, %Y") }} {# November 29, 2026 #}
{{ this.updated|dateformat("%H:%M %z") }}    {# 08:30 +0100 #}

Something that is not a date, or a format the value cannot fill (such as %z for a date-time without an offset), is motabhai::template::invalid_date. There is no time zone database, so date-times print in the offset they were written with.

Markdown

markdown fields and the markdown filter render CommonMark with tables, strikethrough (~~gone~~), task lists (- [x] done) and footnotes ([^1]). Raw HTML in the Markdown passes through as it is.

A fenced code block that names a language is highlighted. The colours are inline styles, so highlighted code needs no stylesheet. Each token has a light colour and a dark one, written as CSS light-dark(), and the page's color-scheme picks between them. With color-scheme: light dark on :root, code follows the reader's system setting; a theme switch that sets color-scheme: dark or light turns the code with it. A page that sets no color-scheme gets the light colours:

```rust
fn main() {}
```

A block with no language, or one that is not known, is left as a plain <pre><code> with its text escaped.

Link and image targets follow the rules of the url filter, so [About](/about) links to that page wherever its slug puts it, ![A photo](photo.jpg) is the page's own attachment, and both get the base path. A markdown field resolves them against the page the field belongs to, wherever a template shows it: site.get("/post").body printed on the home page still links to the post's attachments. The markdown filter resolves them against the page being rendered.

Queries

this.children and site.query(path) are queries. A query does nothing until a template iterates it or asks for a result, so chaining is cheap:

{% for post in this.children.filter(tags__contains="rust").order_by("-pub_date").limit(5) %}
  <a href="{{ post|url }}">{{ post.title }}</a>
{% else %}
  <p>No posts yet.</p>
{% endfor %}

By default a query lists the children in the parent model's children.order_by order (else by _id), leaving out hidden and undiscoverable pages.

MethodWhat it does
filter(field=value, ...)Keeps pages matching every condition. Calls chain, and each adds conditions.
order_by("field", "-other", ...)Replaces the order. A leading - sorts descending. Pages without the field sort first.
limit(n)At most n pages.
offset(n)Skips the first n pages.
include_hidden()Includes hidden pages. include_hidden(false) turns it off again.
include_undiscoverable()Includes undiscoverable pages, likewise.
first()The first page, or none.
count()The number of pages.
all()The pages as a list.

A query is also iterable, works with |length, and is false when empty.

Filter conditions

A condition is a keyword argument: field=value for equality, or field__op=value with one of these operators:

OperatorMatches when the field
field=valueequals the value
field__ne=valuediffers from it; a page without the field always differs
field__lt, field__le, field__gt, field__geis less than, at most, greater than, at least the value
field__contains=valueis a list holding the value, or text containing it
this.children.filter(featured=true)
this.children.filter(pub_date__ge="2026-01-01", tags__contains="rust")
this.children.filter(_model="blog-post")

Dates are compared with ISO 8601 text, such as "2026-02-15". System fields work too. Values can be text, numbers, booleans or lists of text. An unknown operator, a positional argument or any other malformed query is motabhai::template::invalid_query.

Incremental builds and templates

Every page records what it read while rendering: its page, every template it extended or included, every other page, query, databag, asset link and config value it used. The next build re-renders only the pages whose inputs changed. Queries are recorded with their results, so a listing is redone when a page is added to it or removed from it, not when an unrelated page changes. Nothing in a template needs to declare this.