Theme authoring
Write a FerriteCMS theme: theme.toml, templates, the template hierarchy, the context and every helper.
A theme is a folder of MiniJinja templates and static assets. Themes do not run code on the server: templates get data from core and a fixed set of helpers, and anything that needs logic belongs in a plugin. Three themes are built into the binary (blog, docs, business); their folders under themes/ in the repository are good starting points.
The folder
my-theme/
theme.toml
templates/
index.html required: the last fallback for every page
single.html optional, see the hierarchy below
partials/… anything you include or extend
assets/
style.css served at asset('style.css')
Every file under
templates/must be UTF-8 and compile when the theme is loaded; a theme that does not compile is refused.At most 2000 files and 20 MiB in all, folders at most 16 deep. Symbolic links are refused and hidden files (names starting with
.) are skipped.Templates are found by their path under
templates/only:include,extendsandimportcannot reach other files.
theme.toml
[theme]
name = "my-theme" # required: 1 to 64 characters of a-z, 0-9 and -
version = "0.1.0" # required: a semver version
author = "You"
license = "MIT"
homepage = "https://example.com" # http:// or https:// only
description = "One line for the admin"
[menus] # menu locations: name = "label"
primary = "Primary navigation"
footer = "Footer"
[images] # extra image sizes, made for every upload
card = { width = 640, height = 360, crop = true }
[settings] # shown in the admin's Customize panel
accent_color = { type = "color", default = "#2563eb", label = "Accent colour" }
show_toc = { type = "bool", default = true }
Unknown keys are refused.
Menus. Location names are
a-z,0-9and-. A template reads the menu stored under that name withmenu("primary").Images. Size names are
a-z,0-9,_and-. They come on top of the core sizesthumbnail,mediumandlarge; a size with one of those names, a size with aheightbut nocrop, and a size with a zero dimension are skipped. After adding sizes, runferrite media regenerateto make them for older uploads.Settings. Keys are
a-z,0-9,_and-. The types aretext,textarea,bool,number,select(needsoptions = [...]),color(#rgbor#rrggbb) andmedia(a media item; it takes no default).labeldefaults to the key. There is no secret type: every setting is visible to templates. Templates readsite.settings.<key>: the stored value when it is valid, else the default, else the type's empty value.
The template hierarchy
For each page FerriteCMS picks the first template that exists in this list, then falls back to index.html:
Page | Templates tried, in order |
|---|---|
A single item (not a page) |
|
A page |
|
A type archive |
|
A category or tag |
|
Another taxonomy's term |
|
An author |
|
A date archive |
|
Search results |
|
A static front page |
|
The front page as a list of posts |
|
Not found (404) and gone (410) |
|
A server error uses 500.html only, with no fallback to index.html; without it the built-in error page is shown. It renders with very little context, so keep it standalone.
What templates see
Name | Set on | What it holds |
|---|---|---|
| every page |
|
| a single item or page | the item (below) |
| a page | the same object as |
| listings and search |
|
| listings and search |
|
| listings |
|
| every page |
|
On the 404 and 410 page request.path is empty, since one body is shared by every missing address.
An item (post, each of posts.items, and what related() and query() return) has id, kind, slug, title, excerpt, url, body (the rendered HTML, already marked safe), toc, date (published, RFC 3339, or none), updated, menu_order, parent_id, fields (custom fields), author (id, login, display_name), terms (each with taxonomy, slug, name, url, id) and featured (the featured image or none).
post.toc lists every heading of the body in order, as {level, id, text}. Each heading in the body has an id made the way GitHub makes them: lowercase, letters, digits, -, _ and spaces kept, spaces turned into -; a repeated heading gets -1, -2 and so on. An "On this page" list is a loop:
{% for e in post.toc %}{% if e.level == 2 or e.level == 3 %}
<a href="#{{ e.id }}">{{ e.text }}</a>
{% endif %}{% endfor %}
The ids content, comments, respond, search, menu, top, main, toc, nav and footer are never given to a heading, so a theme can use them for its own landmarks.
Output is escaped automatically, except in templates whose names end in .txt, .css or .js.
Helpers
Helper | Returns |
|---|---|
| the page's |
| the HTML plugins add before |
| the URL of a file under |
|
|
| up to |
| public items of a type, at most 50. With |
| the public items of one type as a tree: for a sidebar, breadcrumbs and previous/next links. See below. |
|
|
| one page of the approved comments of a public post, with the form's details; |
| the URL of a plugin's route, |
Filters added to MiniJinja's own:
Filter | Does |
|---|---|
| formats an RFC 3339 time with strftime codes, in |
| plain text of HTML, at most |
| the slug core would make of a text. |
| JSON that is safe inside |
MiniJinja's range, dict and namespace, its standard filters and tests, and urlencode are there too. debug() is removed, and there is no truncate: use excerpt.
page_tree()
page_tree(post) reads the tree of post's type and marks post as the current page; page_tree(kind="page") reads a type's tree without a current page. It returns:
items: the top-level nodes, each with all its descendants inchildren;current,ancestors(top level first, withoutcurrent),children(the current page's children);prevandnext: the pages before and after the current one, reading the whole tree from top to bottom;truncated: true when the type has more than 1000 public items; only the first 1000 are in the tree.
Every node is {id, title, url, depth, current, open, excerpt, featured, children}, with depth from 0 and open true on the current page and its ancestors. The front page's url is /. excerpt is the page's own excerpt, empty when it has none. Siblings are sorted by their order field, then by title. A page whose parent is not public is left out of the tree with everything below it; the page itself still renders. With no type, or a type with no public items, page_tree returns none.
{% set tree = page_tree(post) %}
{% if tree and tree.next %}<a href="{{ tree.next.url }}" rel="next">{{ tree.next.title }}</a>{% endif %}
Limits
A render may execute 5,000,000 template instructions and write at most 8 MiB. The data helpers (menu, related, query, page_tree, image, comments) may be called 20 times per render in all; after that they return nothing. Repeated menu() calls with the same name, and page_tree() calls for the same type, count once. A render that hits the instruction or size limit, like any template error, is logged and answered with a 500 page (the theme's 500.html, else the built-in one); the next render starts afresh.
A minimal theme
templates/index.html:
<!doctype html>
<html lang="{{ site.locale or 'en' }}">
<head>{{ head() }}<link rel="stylesheet" href="{{ asset('style.css') }}"></head>
<body style="--accent: {{ site.settings.accent }}">
<main>
{% if post %}
<h1>{{ post.title }}</h1>
{{ post.body }}
{% elif posts %}
<h1>{{ site.title }}</h1>
{% for p in posts.items %}<p><a href="{{ p.url }}">{{ p.title }}</a> {{ p.date | date("%-d %B %Y") }}</p>{% endfor %}
{% else %}
<h1>Not found</h1>
{% endif %}
</main>
{{ footer() }}
</body>
</html>
with this theme.toml:
[theme]
name = "my-theme"
version = "0.1.0"
[menus]
primary = "Primary navigation"
[settings]
accent = { type = "color", default = "#2563eb", label = "Accent colour" }
Install and activate
With SQLite, stop serve first. Install the folder and make it the active theme:
ferrite theme install --data ./mysite --dir ./my-theme --activate
ferrite theme list --data ./mysite
In the admin, Appearance → Upload theme takes the folder and packs it in the browser. The API (POST /api/v1/themes) takes a bundle, which theme pack makes without a data folder:
ferrite theme pack --dir ./my-theme --out my-theme.tar
pack, install and activate all refuse a theme that does not load. See Themes for activating and customising themes.
Updated