Pages are rendered with Nunjucks templates. coffer ships a default theme; a site’s own theme only has to contain what it changes.
import { createTemplates } from 'coffer';
const templates = createTemplates({
lang: site.lang,
theme: site.theme, // absolute path of the theme directory, or null
baseUrl: site.baseUrl,
timezone: site.timezone,
onMissing: (message) => console.warn(message),
});
const html = templates.render('page.njk', { site, page: { title: 'Hello', content: '<p>Hi</p>' } });
theme is the theme of the site in coffer.yaml.
Theme Layout
my-theme/
templates/ templates that replace or extend the engine's
base.njk
post.njk
i18n/ UI strings, one file per language
en.yaml
de.yaml
Both folders are optional.
Search Order
For a template name such as page.njk, coffer looks in this order and uses the first match:
templates/of the site’s theme;- the engine’s default theme (
theme/templates/in coffer).
A template the theme does not have is therefore taken from the engine, so a theme can start with a single file.
A name with the prefix coffer: skips the theme and always means the engine’s copy: coffer:page.njk. This is what lets a theme file extend the engine template it replaces.
Templates in the engine refer to each other by plain names ({% extends "base.njk" %}), so a theme base.njk also restyles every engine page that extends it.
Overriding One Block
base.njk defines these blocks:
| Block | Contents |
|---|---|
head |
<meta> tags, the theme script, the stylesheet, the stylesheets in the page’s styles list (the viewer’s, on pages with images) and <title>; add stylesheets and feed links here |
theme_script |
the inline script that applies the saved colour scheme (nested in head) |
feed_links |
the <link rel="alternate"> elements that advertise feeds (nested in head) |
header |
the site title and the navigation (inside <header>) |
nav |
the navigation list; nested in header, so it can be replaced alone |
theme_switch |
the light/dark button; nested in header |
main |
the page itself (inside <main id="main">) |
footer |
the content license and the credit line (inside <footer>) |
scripts |
scripts at the end of <body>; by default one <script defer> for each address in the page’s scripts list, which holds the carousel script on pages with a carousel (see Carousels), the scripts of the image viewer on pages with images (see The image viewer) and /assets/video.js on any page whose text has a video (see Videos); before them, a <script type="application/json" id="coffer-viewer"> element with the list of images when the page’s viewerData is not empty |
analytics |
the analytics snippet, after scripts: partials/analytics.njk, empty unless the site configures analytics and the build is not a preview (see Analytics) |
Besides the base templates, the theme layer has these for projects (see Projects): project-index.njk, project.njk, project-tool.njk, project-game.njk, project-card.njk and project-picture.njk.
To change only the footer, create templates/page.njk in the theme:
{% extends "coffer:page.njk" %}
{% block footer %}
<p>{{ site.contentLicense }}</p>
<p>{{ 'footer.credit' | t }}</p>
{% endblock %}
Everything outside the footer block is rendered exactly as before. Use {{ super() }} inside a block to keep the engine’s content and add to it:
{% block head %}
{{ super() }}
<link rel="stylesheet" href="{{ '/theme.css' | url }}">
{% endblock %}
To change a block on every page at once, override it in templates/base.njk:
{% extends "coffer:base.njk" %}
{% block footer %}...{% endblock %}
Colours, Fonts and the Theme Switch
The default stylesheet (assets/coffer.css) takes every colour, font and width from CSS custom properties, so a site theme can restyle the engine’s pages by overriding variables only, with no change to the templates.
| Variable | Light default | Dark default | Used for |
|---|---|---|---|
--coffer-bg |
#ffffff |
#16181d |
page background |
--coffer-text |
#1b1b1f |
#e8e8ec |
body text |
--coffer-muted |
#5a5a63 |
#a9a9b3 |
dates, footer, quotes |
--coffer-accent |
#0b5cad |
#7ab7ff |
links and the focus ring |
--coffer-border |
#d3d3da |
#3a3e48 |
rules, table and code borders |
--coffer-code-bg |
#f3f3f6 |
#20232a |
background of code (unhighlighted) |
--coffer-code-fg |
#1b1b1f |
#e8e8ec |
text of code (unhighlighted) |
--coffer-font-body |
system sans-serif stack | same | body text |
--coffer-font-heading |
the body font | same | headings and the site title |
--coffer-font-mono |
system monospace stack | same | code |
--coffer-measure |
68ch |
same | width of a line of text and of the page frame |
Light values are set on :root. Dark values are set twice, under @media (prefers-color-scheme: dark) (the reader’s system setting) and under :root[data-theme="dark"] (the reader’s choice with the switch). :root[data-theme="light"] sets the light values again, so a reader on a dark system can choose light. Pages follow the system setting without any JavaScript.
A site theme adds its own stylesheet after the engine’s (see Overriding one block, {{ super() }} in head) and repeats the three places for the variables it changes:
:root {
--coffer-accent: #8a2be2;
--coffer-font-heading: Georgia, serif;
}
@media (prefers-color-scheme: dark) {
:root {
--coffer-accent: #c9a0ff;
}
}
:root[data-theme="dark"] {
--coffer-accent: #c9a0ff;
}
:root[data-theme="light"] {
--coffer-accent: #8a2be2;
}
Fonts and the measure are the same in both modes and need only the :root rule.
--coffer-measure is registered as a CSS <length> (@property, inherits: true), so a value in ch is resolved once, with the font size of the root element, and every element sees the same length. Without that, the footer’s smaller text would make the footer narrower than the header. A theme that overrides the measure must set it on :root, as a length (72ch, 40rem, 640px); set on another element, it resolves there and the header, main and footer can differ again.
The Theme Switch
With JavaScript, a button in the header cycles through “system”, “light” and “dark”. The choice is stored in localStorage under the key coffer-theme (nothing is stored for “system”, and a browser that blocks storage simply forgets the choice). A small inline script at the top of <head> reads it and sets data-theme on <html> before the page is painted, so a reload does not flash the wrong theme. The same script adds the class js to <html>; the stylesheet shows the button only then, so without JavaScript there is no button and the page follows the system. The button’s text comes from the UI strings theme.button, theme.system, theme.light and theme.dark.
Override the blocks theme_script and theme_switch to change the script or the button; a theme that replaces the whole head block without {{ super() }} also drops the script.
Code
Highlighted code carries both colour schemes as the variables --shiki-light, --shiki-dark, --shiki-light-bg and --shiki-dark-bg (see Markdown). The default stylesheet picks the pair that matches the active colour scheme in the same three places as above, so code is readable in both modes.
Motion, Small Screens and Print
The default theme defines no animation or transition: state changes are instant. The layout is a single column that works from 360 px wide, with a table of contents beside the text on wide screens. Focus is shown with an outline in the accent colour. Printing always uses black on white, hides the navigation and the switch, and prints the address after external links.
Hanging Punctuation
When a paragraph, heading, list item or quote starts with an opening quote, bracket or sentence dash, the engine wraps that first character in <span class="hang" data-m="quote">. data-m is quote, dash or bracket. The default theme hangs the mark into the margin in two ways:
.hanghasmargin-inline-start: calc(-1 * var(--hang, 0.5em)). The width comes from--hang, set per mark by.hang[data-m="quote"],.hang[data-m="dash"]and.hang[data-m="bracket"].- Text blocks have
hanging-punctuation: first. Inside@supports (hanging-punctuation: first)the.hangmargin is set to0, so a mark never hangs twice.
With hangLines, the engine also wraps an opening mark that follows a space inside a block in <span class="hang-l" data-m="quote">. The theme script /assets/hang-lines.js adds the class is-lead and the width of the mark in --hang-w to the marks that start a line. .hang-l.is-lead moves the mark into the margin by --hang-w and pulls the text after it to the content edge with a negative end margin. The script drops a mark when that shorter line lets a word move up from the next line. A theme that replaces the stylesheet and keeps the option needs this rule and the script, or can leave the span unstyled. Without the script nothing moves.
Lists keep list-style-position: outside, so bullets and numbers already hang. A theme that replaces the stylesheet should keep these rules, or style .hang itself. Without any style the span does nothing and the page is correct. Feeds, the search index and meta text have no hang spans. See Typography.
Title Cards
Posts without a cover and the tag, series and blog index pages are shown in link previews with a generated card (see SEO and link previews). A theme can restyle the card with three optional things in its directory:
my-theme/
theme.yaml colours
card.svg.njk the SVG template of the card
fonts/ .ttf or .otf files for the card
theme.yaml sets the accent colour of the theme and, optionally, the colours of the card. Colours are written as #rgb or #rrggbb; every key may be left out:
accent: "#c9a0ff"
card:
background: "#101014"
text: "#ffffff"
mutedText: "#b0b0c0"
card.svg.njk is a Nunjucks template that must produce an SVG of 1200 by 630 pixels (any other size fails the build). It receives width, height, padding, the colours background, foreground, muted and accent, fontFamily, siteTitle, siteTitleSize, siteTitleY, titleSize and titleLines, a list of { text, y } with the wrapped title and the baseline of each line. Values are escaped. The engine’s theme/card.svg.njk is a working example.
Font files in fonts/ replace the bundled font; the template should use fontFamily, which is the family name read from the font that is closest to bold. The renderer reads only these files, never the fonts installed on the machine. WOFF and WOFF2 files are not supported, and a font should have the Latin and Cyrillic letters the site needs.
Templates of the Blog
| Template | Page | Blocks besides the ones of base.njk |
|---|---|---|
post.njk |
a post | project_badges (the badges of the projects the post is about, above the title), meta (date and reading time), tags, series_top, toc, body, series_bottom, project_blocks (one partials/project-block.njk per project; see Projects), share (the partials/share.njk block of a blog with a share summary, at the very end; see Share summary) |
blog-index.njk |
the list of posts | heading (title and link to the tags), feed_link (the visible link to the page’s feed), item (one entry of the list), pagination (previous and next links; empty for a single page) |
tag.njk |
a tag page | extends blog-index.njk; heading, breadcrumbs, children |
tag-index.njk |
the tag tree | tree |
search.njk |
a blog’s search page | search_nojs (what a visitor without JavaScript reads), search_form (see Search) |
series.njk |
a series page | extends blog-index.njk; heading, breadcrumbs, item (see Series) |
series-nav.njk |
macro used by post.njk |
seriesNav(series, where), called by the series_top and series_bottom blocks of post.njk |
docs.njk |
a page of a project’s documentation | docs_breadcrumbs, docs_sidebar, docs_header, toc, docs_body, docs_pager, docs_source (see Project documentation); the table of contents is the partial partials/toc.njk, shared with post.njk |
reference.njk |
the page of a generated reference in the json format; standalone (does not extend base.njk, deliberately not the site’s look) |
reference_style, reference_body (see The reference) |
map.njk |
partial for a map of a post | rendered by the build once per map and put into the text of the post (see Maps and GPX tracks); receives map (svg, data, tracks) |
home.njk |
the home page | about, links, showcase, latest, secondary (see Home) |
404.njk |
404.html |
head is extended with a noindex tag |
post.njk receives post (slug, url, title, date, tags, tagLinks (the tags as { path, name, url }), summary, draft, unpublished, readingMinutes, series), share (text, url and file of the post’s share summary; null for a blog without one), projectBlocks (the projects the post is about, each { slug, name, pitch, url, kind, docsUrl, storeLinks, feed, posts }; empty for most posts), series (the same object as post.series: slug, name, url, number, count, previous and next as { title, url }; null when the post is in no series), toc (a list of { id, text, depth }, empty for short posts), blog and page.content (the rendered body). blog-index.njk receives pagination (page, pages, previous and next, the addresses of the neighbouring pages or null; null for a list that is a single page; tag.njk and series.njk receive it too), posts, a list of the same post objects, newest first (with tagLinks filled, and two more fields, index and html, described below), tagsUrl, the address of the tag tree, and pageFeed (url, title), the feed that belongs to the page (the blog’s, the tag’s or the series'). Every page template receives feedLinks, a list of { url, title }, which base.njk turns into <link rel="alternate" type="application/atom+xml"> elements in the feed_links block of the head; see Feeds. tag.njk receives the same, plus tag (path, name, description, url, count), ancestors ({ path, name, url }, top down) and children ({ path, name, description, url, count }); tag-index.njk receives tree, the nested list of the same tag objects with their children. See Tags. Static files of a theme go in its assets/ folder and are copied to assets/ of the output (see Building a site).
On the pages that list posts (the blog index, the tag pages and the series pages), each post has index, the effective setting of the post (summary or full: the post’s own index front matter, else the blog’s index setting; see Configuration), and html. For a post whose index is full, html is the whole body of the post, ready to put into the page with {{ post.html | safe }}; for a summary post it is an empty string. The body is rendered as on the post page (images, code, tables, carousels, maps, footnotes), with two changes so that several posts fit on one page below the h1 of the page and the h2 of each title: every heading is one level lower (an h2 of the text is an h3, an h6 stays an h6), and every id, footnote and in-text #id link starts with the slug of the post and a hyphen (my-trip-day-two), so that no two posts on a page share an id. The table of contents, the series box, the project blocks and the share summary are not part of html; a post page is not changed. The default blog-index.njk shows a full post as the title, the meta line, the tags and <div class="post-body">{{ post.html | safe }}</div> in the same li; the item block can be overridden to lay it out differently. The block is not marked for the search: the post page is what coffer indexes, so a search finds each post once.
A post that is in the build only because of preview mode (post.unpublished: a draft, or a post dated in the future) is marked by the partial partials/banner.njk, which post.njk, blog-index.njk and home.njk include wherever the post is shown. It prints the strings post.banner_draft (“Draft”) or post.banner_scheduled (“Scheduled for {date}”) in a <p class="preview-note preview-banner">; a theme restyles it with CSS or replaces the partial (see Authoring).
To change only the line with the date and reading time of a post:
{% extends "coffer:post.njk" %}
{% block meta %}<p>{{ post.date | date('long') }}</p>{% endblock %}
Variables
The engine’s pages receive site (title, content license, links, …), page (title, description, content, …) and, for the navigation, nav, a list of { label, url } or { key, url } items (key is a UI string key). Without nav, the navigation holds a link to the home page. lang and basePath are available in every template.
Values are escaped. Mark HTML you trust with | safe.
Filters
treturns a UI string:{{ 'nav.home' | t }}. Parameters are passed as an object:{{ 'post.series_part' | t({ n: 2, m: 5 }) }}gives “Part 2 of 5”.dateformats a date or a moment in the site’s language and time zone:{{ post.date | date }}. The format isshort,medium(default),long,full,iso(2024-05-01, fordatetimeattributes) or an object ofIntl.DateTimeFormatoptions:{{ d | date({ month: 'long', year: 'numeric' }) }}. A date without a time (2024-05-01) is never shifted by the time zone.urlturns a site path into a link that works on the published site: it adds the path of the site’sbaseUrl(for a site served below/docs) and a trailing slash, keeps any query and fragment, and leaves files (/feed.xml), absolute URLs (https:,mailto:),#fragmentsand relative paths alone.{{ '/devlog' | url }}gives/devlog/.
UI Strings
Words in the templates (navigation labels, “Part 2 of 5”, reading time, feed titles, the 404 text) come from YAML files named after the language, for example i18n/en.yaml and i18n/ru.yaml in the engine. The language is the lang of the site. A regional tag such as pt-BR reads pt.yaml and then pt-BR.yaml.
nav:
home: Home
post:
series_part: "Part {n} of {m}"
reading_time:
one: "{count} minute"
other: "{count} minutes"
- Keys are nested: the first group above is the key
nav.home. {name}in a string is replaced by the parameter of that name.- A string with plural forms is a mapping of plural categories (
zero,one,two,few,many,other) and needs acountparameter:{{ 'post.reading_time' | t({ count: post.readingMinutes }) }}. The category comes fromIntl.PluralRulesfor the site’s language, so Russian hasone(1, 21),few(2, 22),many(5, 11) andother(fractions).
A theme overrides strings with its own i18n/{lang}.yaml. It lists only the keys it changes; the rest keep the engine’s text.
Missing Strings
A key that has no string in the site’s language is taken from English and reported once, for example ru: missing string "nav.search"; using English. A key that does not exist even in English is shown as the key itself and reported too. Pass onMissing to createTemplates, or read templates.missingStrings() after rendering, to collect the messages for the build report.
Adding a Language
- Copy the engine’s
i18n/en.yamlinto the theme asi18n/{lang}.yaml, for examplei18n/de.yaml. - Translate the values; keep the keys. For strings with plural forms, give the forms the language needs (see the CLDR plural rules);
otheris the form used when no other applies. - Set
lang: defor the site incoffer.yaml.
Strings you leave out fall back to English and show up in the missing-strings report, so a translation can grow one string at a time. To add a language to the engine itself, put the same file in the engine’s i18n/ folder.