Guidedog Manual 1.0.0-beta.16
Language
On this page
Guidedog / Documentation 1.0.0-beta.16

Templates

A template controls presentation. It does not duplicate the source prose. Guidedog uses Jinja for HTML and Typst for PDF. For a complete worked project, follow Build a project theme. This chapter is the reference for its settings, blocks and parameters.

Templates in Guidedog are ordinary project files. We can see them and edit them. When we start a project with guidedog quickstart, it writes the working templates into our project:

  • _templates/layout.html defines the base HTML website structure (Jinja), while built-in article.html, collection.html, and landing.html extend it for articles, archives, and landing pages.
  • _templates/theme.typ defines the shared PDF palette, fonts and language-aware content wrapper.
  • _templates/discussion.typ defines the discussion record layout (Typst).
  • _templates/article.typ defines the standalone print-quality article layout for pages with :pdf: article (Typst).
  • _templates/book.typ defines the PDF book layout, typography, and cover page (Typst).
  • _static/guidedog.css, _static/guidedog.js, and _static/typography.js supply default styles, client behaviors, and browser paragraph line breaking.

Templates live in the project directory or in a content-addressed pinned theme archive. They are not hidden in an external package or binary cache. We edit them directly, rebuild with guidedog build, and preview changes with guidedog serve.

HTML templates

Guidedog renders layout.html (or a page’s selected :template: such as article.html or landing.html) using its built-in Jinja engine. Split a layout into partials and macro libraries. Inherit shared blocks from a base layout. Each file then has one job.

Project templates override pinned theme archives and built-in layouts, composing partials and macros into complete HTML pages.
Fig. 15 Three-tier template lookup, inheritance, and block composition in Guidedog.

Modular template structure

A project can organize templates across any number of files and subdirectories under the directories listed in templates_path (by default, ["_templates"]):

my-docs/
|-- conf.toml
|-- index.rst
`-- _templates/
    |-- layout.html
    |-- base.html
    |-- partials/
    |   |-- header.html
    |   |-- navigation.html
    |   |-- searchbox.html
    |   `-- footer.html
    `-- macros/
        `-- components.html

Guidedog scans all files and subdirectories within templates_path. Any file or partial can be included or imported using paths relative to templates_path:

Listing 10 _templates/layout.html
{% extends "base.html" %}

{% block header %}
  {% include "partials/header.html" %}
{% endblock %}

{% block footer %}
  {% include "partials/footer.html" %}
{% endblock %}

Jinja macros can also be defined in separate files and imported wherever needed:

Listing 11 _templates/macros/components.html
{% macro badge(label, type="info") %}
  <span class="badge badge-{{ type }}">{{ label }}</span>
{% endmacro %}
Listing 12 Using macros in any template
{% import "macros/components.html" as ui %}
{{ ui.badge("New", type="success") }}

Theme inheritance with !layout.html

Sometimes we want to change only some sections of the default theme, not write the whole page structure from scratch. Then we inherit from Guidedog’s built-in theme layout with Sphinx’s exclamation mark syntax:

Listing 13 _templates/layout.html
{# Inherit Guidedog's built-in layout #}
{% extends "!layout.html" %}

{# Inject custom metadata or web fonts into the document head #}
{% block extrahead %}
  {{ super() }}
  <link rel="stylesheet" href="{{ pathto('_static/custom.css', 1) }}">
{% endblock %}

{# Replace or extend the footer with custom content #}
{% block footer %}
  {% include "partials/footer.html" %}
{% endblock %}

{% extends "!layout.html" %} (or {% extends "basic/layout.html" %}) tells the loader to fetch the theme’s underlying template. Without the mark, it would load the project’s own _templates/layout.html again, recursively.

Inside any overridden block, {{ super() }} renders the parent block’s default markup. So we can add markup before or after it without copying existing code.

Layout blocks

Guidedog’s built-in layout.html defines blocks matching Sphinx’s conventions:

Block Purpose
doctype Document type declaration (default: <!DOCTYPE html>).
htmltitle The <title> element inside <head>.
linktags Navigation and meta link tags (favicon, index, search, prev, next).
css Stylesheet link tags and inline CSS root variables.
scripts JavaScript tags, including search indexing and interactive features.
extrahead Empty insertion point at the end of <head> for analytics, fonts, or custom meta tags.
header Header slot rendered immediately before the main navigation bar.
relbar1 Top navigation bar containing branding, version badge, search box, and theme toggle.
rootrellink Insertion point in navigation bar before related links.
relbaritems Custom items inserted into the navigation bar.
sidebar1 Left-hand navigation sidebar container.
sidebartoc Table of contents navigation tree rendered inside sidebar1.
breadcrumbs Hierarchical breadcrumb navigation path above the article content.
document Article wrapper enclosing the page body.
body Rendered HTML content of the current document ({{ body }}).
relbar2 Bottom pagination controls offering previous and next chapter links.
footer Page footer containing copyright notice, last updated timestamp, and source links.
sidebar2 Right-hand secondary rail containing the local page outline (“On this page”).

Finding templates

Guidedog searches three tiers in order:

  1. Each project directory listed in conf.toml under templates_path (by default ["_templates"]).
  2. The verified pinned theme directory _themes/<tree-sha256>/_templates when site.theme = true.
  3. Guidedog’s embedded built-in templates (layout.html, page.html, article.html, collection.html, landing.html, search.html, genindex.html, modindex.html).

Template names are relative to the template directories. They cannot escape those directories with parent directory traversal (..).

During rendering, the Jinja environment records the content digest of every template, parent layout, included partial, and imported macro file that each page loads. When a template or partial file is edited, guidedog build and guidedog serve rebuild only the pages whose template chain loaded that file. Editing article.html rebuilds articles without rebuilding documentation chapters that render layout.html directly; editing layout.html rebuilds every page that extends it.

Page variables

Where Sphinx has a name for a variable, Guidedog uses it, so parts of Sphinx themes carry over.

Variable Value
body The document, as HTML.
title The document’s title.
pagename, docname The document’s name, such as usage/install. On a page that is no document’s, docname is empty and pagename is the page’s name, as in Sphinx: genindex, py-modindex, search, or the name of a page made from a template.
toc The navigation built from the toctrees, as HTML.
outline The document’s own sections, as HTML; empty when it has none.
prev, next The neighboring pages in reading order, with url (also link) and title; none at either end.
page page.meta contains date, updated, publish_at, expires_at, draft, authors, category, tags, series, summary, template, pdf, pdf_url and nosidebar. Dated pages also provide page.word_count, page.reading_minutes, page.older, page.newer and page.taxonomies. Neighbors contain title, url and date, or are None at either end. Each enabled taxonomy maps to a list of name and url entries.
collection Generated archive pages provide collection.title, collection.output, collection.page (one-based), collection.pages, collection.previous_url, collection.next_url and collection.posts. Each post contains title, docname, date, url, summary, excerpt_html and meta. Previous and next URLs are relative, or None at either end. Collection feeds use RSS and Atom.
project, version, release, copyright, language The settings of the same names.
html_title, docstitle html_title, by default “<project> <release> documentation”.
html_short_title, shorttitle html_short_title.
root_doc, master_doc The root document’s name.
pathto_root The path from the page to the site’s root, such as ../.
root_url, search_url, genindex_url Links to the root page, the search page, and the general index.
css_files, js_files Lists of files with a url: Guidedog’s own, then html_css_files and html_js_files.
logo_url, favicon_url html_logo and html_favicon below _static; empty when unset.
accent html_theme_options.accent, the theme’s color.
sourcelink_url The document’s source below _sources, when it is shown; otherwise empty.
last_updated The build date in html_last_updated_fmt when it is set; otherwise empty.
show_copyright, show_sphinx, show_guidedog, has_source, show_source The html_show_* and html_copy_source settings.
builder, file_suffix The builder’s name, such as html, and the page suffix.
every key of html_context Its value, with its type, as Sphinx passes a Python value: false is false in {% if %}, numbers are numbers, arrays are lists, and tables are dicts whose keys keep the order conf.toml gives them. guidedog migrate carries the literal entries of conf.py’s html_context. A value conf.py computes is left undefined, which a template reads as false.

pathto is Sphinx’s template function. pathto("usage/install") is the URL of a document’s page, and pathto("_static/logo.svg", 1) the URL of a file below the site’s root, both relative to the current page. With the singlehtml builder, as with Sphinx’s, a document is its section of the single page: pathto("usage/install") is #document-usage-install on that page and index.html#document-usage-install on the index and search pages. A name that is no document’s, such as genindex or a page made from a template (below), is a page at the site’s root with every builder.

Pages made from templates

html_additional_pages makes pages from a template alone, as in Sphinx. Each key is a page’s name and each value a template in templates_path:

Listing 14 conf.toml
root_doc = "contents"

[html_additional_pages]
index = "indexcontent.html"
download = "download.html"

A page’s template usually extends layout.html and fills its blocks, so the page looks like every other one:

Listing 15 _templates/indexcontent.html
{% extends "layout.html" %}
{% block htmltitle %}<title>{{ shorttitle }}</title>{% endblock %}
{% block body %}
  <h1>{{ docstitle|e }}</h1>
  <p><a href="{{ pathto("tutorial/index") }}">Tutorial</a></p>
{% endblock %}

The template sees the variables of a page that is no document’s: pagename is the page’s name (index), title and body are empty, and pathto, toc, toctree(), and the values of html_context work as on any page. The page is written at the site’s root as <name>.html (html_file_suffix) by html, dirhtml, and singlehtml, and written again on every build. A change to its template rebuilds that page, and a change to a shared base template rebuilds every page that extends it.

Where the page goes, and where it differs from Sphinx:

  • A page named as a document takes that document’s place, as in Sphinx, where it is written last. index = "landing.html" makes the landing page index.html, while index.rst still gives the table of contents its entries. With singlehtml the single page keeps its place and the template page is not written, with a warning.
  • dirhtml writes the page as download.html, beside genindex.html and search.html, where Sphinx writes download/index.html. pathto("download") gives that file.
  • A name must be a file name at the site’s root: "sub/page" is refused with a warning (build.additional_page), since the page’s links are made from the root. So is a name whose file the site already has, such as genindex, where Sphinx would write one of the two over the other.
  • guidedog migrate carries html_additional_pages over from conf.py.

Errors

A mistake in a template stops the build with the template’s file and line, the line itself, and a suggestion:

TEMPLATE ERROR                                                  template.error

  _templates/base.html:1

No filter named 'defualt'.

Did you mean the filter 'default'?

A variable that is not defined renders as nothing, as in Jinja.

Runaway templates stop with an error too, never a crash. The limits are: syntax nested more than 100 levels deep (brackets, tags, and each link of a long chain of operators, filters or elif branches); macros, includes and recursive loops more than 200 deep; and any recursion needing more than 512 KiB of stack. A list that contains itself prints as [...], as in Python.

PDF templates

The PDF book is typeset by Typst. _templates/book.typ is an ordinary Typst file that defines a book function. Guidedog writes the book’s source as

#import "/_templates/book.typ": book
#show: book.with(title: ..., author: ..., version: ..., date: ...,
  lang: ..., paper: ..., numbering: ..., logo: ...)
// the chapters, one per document of the root toctree

so everything that decides how the book looks is in that file: fonts, page size and margins, headings, the title page, running heads, and the table of contents.

Standalone discussion records select _templates/discussion.typ instead. Its built-in RFC-style layout opens with a metadata panel, state badges, a status note and compact contents.

Standalone articles marked with :pdf: article select _templates/article.typ and emit a PDF beside their HTML page. The template defines a book function accepting title, lang, paper, date, updated, category, summary, authors, tags and the content body. A copied book.typ does not override discussion.typ or article.typ. We can customize each kind of PDF independently. Keep a discussion catalog describes the discussion template’s additional record parameter, and Static websites and articles describes standalone article PDFs.

Parameters

Parameter Value
title The book’s title in pdf_documents, otherwise project.
author The book’s author, otherwise author.
version release.
date |today|: today when it is set, otherwise the build date in today_fmt.
lang language.
paper "a4", or "us-letter" when pdf_paper_size is letter.
numbering true when a toctree has the numbered option.
logo The path of pdf_logo; absent when it is unset.
copyright copyright, for the colophon. It is passed only to a template that takes it.
body The chapters.

A template may add parameters with defaults of its own. The template that quickstart writes has accent, the fonts serif, sans, and mono, the text size, and page-ref. When page-ref is set to a function such as n => [p. #n], cross-references to other pages are followed by their page number.

Modular Typst templates

The default book, discussion, and article layouts share theme.typ. It defines gd-palette, the font defaults and configure-document(body, ...). The wrapper applies its rules to the content it receives. The default palette uses Guidedog teal (#126d78), dark ink and light neutral panels. Books use a centered cover and teal chapter openings. Discussion records use metadata panels, state badges, a status note and compact contents. Standalone articles use a masthead with authors, publication date, abstract block, and article headings without chapter counters.

guidedog quickstart copies theme.typ, book.typ, discussion.typ and article.typ. guidedog dsc init copies the shared theme and discussion layout. All four files are embedded in the executable. Without a project layout, the builder composes a self-contained built-in template; it does not read a project theme. Untrusted builds use that same isolated fallback.

We can edit theme.typ to change all three copied layouts together. Keep or update // guidedog: margin-x = 31mm in the leading 256 lines when changing horizontal page margins so native lilypond scores match the text column; see Music notation. Changes to files beneath _templates/, including nested imported modules and assets, invalidate cached PDFs. The builder tracks directory membership as well, so adding or removing a module cannot leave a stale PDF. Template discovery stays inside the project’s readable roots and permits at most 16,384 template inputs.

To adopt a new default in an older project, we first keep a backup of our custom files. We can remove the copied layout to use the embedded version, or copy fresh files from a temporary guidedog quickstart project. We keep theme.typ beside the new layouts. A lone copied layout that imports a missing theme produces a Typst error. Guidedog never overwrites an existing customized template during a build.

Like HTML templates, Typst templates are not restricted to a single file. We can split book styling, macros, and custom covers into several .typ files under _templates/. Standard Typst #import and #include statements put them together:

Listing 16 _templates/book.typ
#import "cover.typ": title-page
#import "typography.typ": apply-styles

#let book(
  title: "",
  author: "",
  version: "",
  date: "",
  lang: "en",
  paper: "a4",
  numbering: false,
  logo: none,
  body,
) = {
  apply-styles([
    #title-page(title: title, author: author, version: version, logo: logo)
    #body
  ])
}

Here apply-styles(body) must apply its rules around the content it receives. A helper that sets text and returns nothing cannot change its caller’s later body. The complete tutorial shows a working wrapper around the generated book template.

Page breaks in captioned content

Guidedog’s shared Typst preamble lets captioned tables and code listings continue across pages. Table headers repeat on each table page. This also applies to existing copies of the book template and to standalone PDF conversion.

When we customize their layout, a breakable table or code block must remain inside breakable containers. An unbreakable enclosing figure can otherwise force its contents past the page boundary. Image figures keep their existing layout behavior. PDF pagination review records the root cause and regression checks.

What a template can use

Typst packages

#import "@preview/cetz:0.4.2" and other packages work, downloaded on first use. Set pdf_packages = "offline" to build only with packages already on disk.

Fonts

The system’s fonts are available, as with the typst command. Add folders with pdf_font_paths, or set pdf_fonts = "embedded" to use only the fonts built into Typst, for byte-for-byte reproducible books.

A preamble

pdf_preamble names a Typst file whose content follows the #show: book line. It suits a few set and show rules without a template of our own.

Target-specific markup and raw blocks

Documents are reStructuredText and Markdown. Typst files and HTML fragments are not documents. To insert specialized markup into either output format without contaminating the other, wrap the block in an .. only:: guard with a matching .. raw:: directive:

.. only:: html

   .. raw:: html

      <div class="interactive-demo">
        <button onclick="runSample()">Test Endpoint</button>
      </div>

.. only:: pdf

   .. raw:: typst

      #align(center)[#text(size: 14pt, weight: "bold")[Typeset only in the book]]

.. only:: html limits interactive web components and custom HTML to web builds. .. only:: pdf keeps vector formatting, callouts, and native Typst expressions to the book (.. only:: latex is recognized as an alias for compatibility with Sphinx projects). Because Guidedog typesets books with Typst rather than LaTeX, raw blocks for the book must use .. raw:: typst; verbatim .. raw:: latex is not supported and is omitted by the PDF builder. To build reusable, semantic directives, see Custom directives and dynamic content and Deliver format-specific content and custom directives.

A function defined in theme.typ is scoped to that module. Document-level raw Typst must import the function explicitly, or use an import in pdf_preamble. A book template’s private imports do not make helpers visible in the generated document. Raw Typst follows the raw-content policy; untrusted builds omit it.

When Typst reports an error, the build names the file and line, whether it is in our template or in the generated book. Each PDF has its own generated source at _build/pdf/sources/<pdf-filename>.typ. For example, reference-en.pdf has sources/reference-en.pdf.typ. A single-book build also keeps the familiar _build/pdf/book-<language>.typ inspection copy. A failed build publishes nothing. Its source remains in _build/.doctrees/failed at the path named by the error. Several books keep distinct failure sources.

Writing a new template

We start from the copies quickstart wrote. They show every part in use:

  1. Keep the layout.html markup that the default style sheet expects, or replace _static/guidedog.css along with it.
  2. Move repeated parts into files of their own and include them, or put a base layout in base.html and extends it from layout.html.
  3. For the book, change the book function’s rules, or write a new function with the same parameters and keep its name book.

Preview with guidedog serve while editing. Save the source and refresh the browser. The server rebuilds changed inputs before serving the page.