Layouts

Tola has no special template inheritance syntax—layout reuse works through ordinary Typst functions. Validate, filter, and sort all-sources() once into a unified pages list, then pass each page through a shared shell and kind-specific templates.

Base layout

Put <html>, <head>, header, and footer into a single base-page(...) function that accepts page-specific parts as named arguments:

typst
// templates/base.typ
#import "@tola/address:0.0.0": asset-url, output-url
#import "@tola/site:0.0.0": site

#let base-page(
  page,
  nav-pages: (),
  head-extra: (),
  sidebar: none,
  body,
) = {
  document(page.output, format: "html", title: page.meta.title)[
    #html.html(lang: site.language.tag)[
      #html.head[
        #html.meta(charset: "utf-8")
        #html.meta(name: "viewport", content: "width=device-width, initial-scale=1")
        #html.title[#page.meta.title | #site.title]
        #html.link(rel: "stylesheet", href: asset-url("/styles/site.css"))
        #for tag in head-extra { tag }
      ]
      #html.body[
        #html.header[
          #html.nav(aria-label: "Primary navigation")[
            #for item in nav-pages {
              let attrs = (href: output-url(item.output))
              if item.output == page.output { attrs.insert("aria-current", "page") }
              html.a(..attrs, item.meta.title)
            }
          ]
        ]
        #html.div(class: "shell")[
          #if sidebar != none { sidebar }
          #html.main(id: "article", body)
        ]
      ]
    ]
  ]
}

Marking the main element with id: "article" gives @tola/feed a clean full-text boundary and keeps chrome out of backlink queries.

Dispatch by kind

When a site combines a home page, documentation pages, blog posts, and standalone pages, derive a kind field from route-segments (or metadata) and dispatch to separate layout functions.

typst
#import "@tola/address:0.0.0": join-route, output-of
#import "@tola/schema:0.0.0" as s
#import "@tola/source:0.0.0": all-sources, parse-sources

#let base-schema = s.schema((
  title: s.non-empty(s.trim(str)),
  description: s.optional(str, default: ""),
  order: s.optional(int, default: 0),
  draft: s.optional(bool, default: false),
  permalink: s.optional(str, default: none),
))

#let post-schema = s.schema((
  title: s.non-empty(s.trim(str)),
  description: s.optional(str, default: ""),
  published: s.date,
  tags: s.optional(array, default: ()),
  draft: s.optional(bool, default: false),
))

#let page-kind(segments) = {
  let first = segments.at(0, default: none)
  if first == none { "home" }
  else if first == "docs" { "doc" }
  else if first == "blog" { if segments.len() == 1 { "blog-index" } else { "post" } }
  else { "page" }
}

#let select-pages() = {
  let pick-schema(source) = {
    if page-kind(source.route-segments) == "post" { post-schema } else { base-schema }
  }
  parse-sources(all-sources(), pick-schema)
    .filter(source => not source.meta.draft)
    .map(source => {
      let route = source.meta.at("permalink", default: none)
      let final-route = if route != none { route } else { join-route(source.route-segments) }
      (
        ..source,
        key: join-route(source.route-segments),
        kind: page-kind(source.route-segments),
        output: output-of(final-route),
      )
    })
}
typst
#import "site/pages.typ": select-pages
#import "templates/base.typ": base-page
#import "templates/doc.typ": doc-page
#import "templates/post.typ": post-page

#let pages = select-pages()
#let nav-pages = pages.filter(p => p.kind in ("home", "blog-index", "page"))
#let docs = pages.filter(p => p.kind == "doc")
#let posts = pages.filter(p => p.kind == "post")

#for page in pages {
  if page.kind == "doc" {
    doc-page(page, docs, nav-pages: nav-pages)
  } else if page.kind == "post" {
    post-page(page, posts, nav-pages: nav-pages)
  } else {
    base-page(page, nav-pages: nav-pages)[#include page.file]
  }
}

Because parse-sources accepts a function, different directories can enforce different schemas (for example requiring published under content/blog/ while keeping it absent on docs pages).

Rule scope

set and show rules follow lexical scope:

  • Top-level rules outside document(...) (such as set text(lang: ...)) apply site-wide.
  • Rules inside #html.main[...] apply only to the article body (code highlighting, heading anchors, link rewriting), leaving headers, sidebars, and footers untouched.
typst
#html.main(id: "article")[
  #show heading: it => render-heading(it)
  #show raw: render-code.with(dark-theme: code-themes.github-dark)
  #include page.file
]

Passing title to document(output, format: "html", title: ...) sets the document title automatically; #title() in the body renders <h1>, and = headings export as <h2>.

Notes

  • Filter drafts once when building pages; never filter at render time while reading all-sources() again for navigation or RSS.
  • Mixing line breaks and html.elem(...) inside markup [...] can wrap controls in <p> tags; assemble nav bars, button groups, and toolbars in code mode { ... }.
  • Reset custom state(...) values (such as pending code-line options) at the start of each document so state cannot leak across pages.

Search this site

Use the arrow keys to move through results.

Type to search.