# Template

The template is the HTML shell around each page. Elysium uses its own unless
you give `template`: `--template`, `"template"` in `elysium.json`, or the Vite
plugin's `template`. Point it at a file to wrap every page, or at a
[folder](#a-template-per-page) that pages pick from. A `_templates` folder
beside the pages is used without being named.

The template is a [Go template](https://pkg.go.dev/text/template). A
placeholder is a field, written with a leading dot: `{{.title}}`. Elysium never
reads text in the page as template code. A placeholder Elysium does not know
fails the build, so a typo never reaches readers.

## Placeholders

| Placeholder           | Value                                                                            |
| --------------------- | -------------------------------------------------------------------------------- |
| `{{.lang}}`           | `lang`, such as `en`                                                             |
| `{{.title}}`          | the page title, escaped                                                          |
| `{{.documentTitle}}`  | `Install — Acme` for the `<title>`, or the title alone when it is the site name  |
| `{{.description}}`    | the page description, escaped, or empty                                          |
| `{{.name}}`           | the site name, escaped                                                           |
| `{{.url}}`            | `siteUrl` plus the page route                                                    |
| `{{.home}}`           | the route of the root page                                                       |
| `{{.base}}`           | the base, such as `/docs`, or empty                                              |
| `{{.markdown}}`       | the route of the Markdown copy                                                   |
| `{{.markdownLink}}`   | a "View as Markdown" button that opens the copy                                  |
| `{{.image}}`          | the URL of the page's social card; empty without `og`                            |
| `{{.updated}}`        | a `p.elysium-updated` with the last commit date                                  |
| `{{.nav}}`            | a `details` menu for phones and a sticky `nav`, each with the search button      |
| `{{.search}}`         | the search button alone, for a page without `{{.nav}}`                           |
| `{{.content}}`        | the rendered page; wrap it in `.charon`                                          |
| `{{.contentsInline}}` | "On this page" for narrow screens, to put above the content                      |
| `{{.contents}}`       | the "On this page" column                                                        |
| `{{.footer}}`         | the previous and next links                                                      |
| `{{.meta}}`           | the icon, description, Open Graph and Twitter tags, with the card if `og` is set |
| `{{.head}}`           | extra head tags: the KaTeX stylesheet, and the `font` stylesheet and family      |
| `{{.frontmatter}}`    | the page's [frontmatter](#frontmatter), every string escaped                     |
| `{{.labels}}`         | the [labels](labels.md), escaped, such as `{{.labels.search}}`                   |

Search and <kbd>Ctrl</kbd> <kbd>K</kbd> work on a page that has the search
button, from `{{.nav}}` or `{{.search}}`.

## The default

Elysium's own template:

```go-template
<!doctype html>
<html lang="{{.lang}}">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>{{.documentTitle}}</title>
    <link rel="canonical" href="{{.url}}" />
    <link rel="alternate" type="text/markdown" href="{{.markdown}}" />
    <link rel="stylesheet" href="{{.base}}/_elysium/docs.css" />
    {{.meta}}
    {{.head}}
    {{block "elysium-head" .}}{{end}}
    <script type="module" src="{{.base}}/_elysium/docs.js"></script>
  </head>
  <body>
    {{block "elysium-body" .}}
    <main class="elysium-layout">
      {{.nav}}
      <div class="elysium-main">
        <article class="charon">
          <div class="elysium-title">
            <h1>{{.title}}</h1>
            {{.markdownLink}}
          </div>
          {{.contentsInline}}
          {{.content}}
        </article>
        {{.updated}}
        {{.footer}}
      </div>
      {{.contents}}
    </main>
    {{end}}
  </body>
</html>
```

The package ships it as `templates/elysium.html`, with its layout in
`docs-layout.css`. It follows the reader's light or dark system theme and has
no theme button; [Light and dark](labels.md#light-and-dark) shows how to add
one.

### Change part of it

Every template can call the default as `elysium` and fill its two blocks:
`elysium-head` goes at the end of the `head`, and `elysium-body` replaces the
docs layout. This template adds a stylesheet and keeps the rest:

```go-template
{{template "elysium" .}}
{{define "elysium-head"}}<link rel="stylesheet" href="/brand.css" />{{end}}
```

To change more, copy the default and edit it.

## The top page

Elysium ships a second template, `top`, for a site's front page: the title and
description set large, a link to start reading, the search button, then the
page. A page picks it in frontmatter, with no template folder:

```md
---
template: top
start: docs/install
---
```

`start` is the page the link opens, from the base; a page without it gets no
link. The link reads `Get started →`, the [`start` label](labels.md). The
package ships the template as `templates/top.html`, and `docs-layout.css`
styles it: `.elysium-top` centers the page, `.elysium-top-hero` sets the title
and description large, and `.elysium-top-actions` lines up the link and the
search button. A `top.html` in a [template folder](#a-template-per-page)
replaces it.

## Logic

Every placeholder but `{{.frontmatter}}` and `{{.labels}}` is a string, and an empty one is
false, so the template can branch on it:

```go-template
{{with .description}}<meta name="description" content="{{.}}" />{{end}}
{{if .image}}<meta property="og:image" content="{{.image}}" />{{end}}
```

`{{define}}` and `{{template}}` name a block and reuse it, `{{- ` and ` -}}`
trim the white space beside an action, and `{{/* a note */}}` is a comment.
Values hold finished HTML, escaped where the table says so: print them as
they are.

## A template per page

Make the template a folder:

```text
docs/
  _templates/
    _base.html   the shell the templates share
    page.html    the docs layout
    top.html     the top page
    blog/
      post.html  a blog post
```

A page names its template in frontmatter, by its path in the folder:

```md
---
template: top
---
```

`template: blog/post` picks `blog/post.html`; the `.html` is optional. The
other pages use `page.html`, and Elysium's own template when the folder has no
`page.html`, so a folder with only `top.html` changes the top page alone. A
page can still pick Elysium's [`top`](#the-top-page) when the folder has no
`top.html`.

Files that start with `_` are shared: every template can call them by their
path, and a page cannot pick one. Leave a `{{block}}` open in a shared file and
fill it from each template:

```go-template
{{/* _base.html */}}
<!doctype html>
<html lang="{{.lang}}">
  <head>
    <title>{{.documentTitle}}</title>
    <link rel="stylesheet" href="{{.base}}/_elysium/docs.css" />
    {{.meta}}
    {{.head}}
    <script type="module" src="{{.base}}/_elysium/docs.js"></script>
  </head>
  <body>
    {{block "body" .}}{{end}}
  </body>
</html>
```

```go-template
{{/* top.html */}}
{{template "_base.html" .}}
{{define "body"}}
<main class="elysium-top">
  <header class="elysium-top-hero">
    <h1>{{.name}}</h1>
    <div class="elysium-top-actions">{{.search}}</div>
  </header>
  <article class="charon">{{.content}}</article>
</main>
{{end}}
```

Each template gets its own copy of the shared files, so the `body` that
`top.html` defines never reaches a page on `page.html`. A template prints what
it holds outside its `{{define}}`s, here the call to `_base.html` and the
line breaks around it, so a template that calls no shared file is a whole
page of its own.

The build checks every template and lists each problem at once: a page that
names a template neither the folder nor Elysium has, or a shared one; and a
template that does not parse, even one no page uses yet. The folder holds templates only: Elysium reads its
`.html` files, and serves nothing from it when it sits inside the docs. Put
stylesheets and images in the [assets](build.md) folder.

## Frontmatter

`{{.frontmatter}}` holds every field of the page's frontmatter, with lists and
nested fields as they are and every string escaped. A top page can keep its
words in its Markdown:

```md
---
template: top
start: docs/install
---
```

```go-template
<a href="{{.base}}/{{.frontmatter.start}}">Get started</a>
```

A field the page lacks fails the build, like a placeholder Elysium does not
know. Where a field is optional, read it with `index`:
`{{with index .frontmatter "start"}}…{{end}}`.

## Layout

`{{.nav}}`, the main column and `{{.contents}}` are three siblings, so a
three-column grid lays them out. `docs-layout.css` does that, and stacks them
on narrow screens.

Keep `{{.head}}` in the `head`: pages with math put KaTeX's stylesheet there,
and `font` puts its stylesheet and family there.
With the CLI, keep the `_elysium/docs.css` and `_elysium/docs.js` tags too, or
add your own.

## Turn off keys

The CLI's script reads two switches from the `html` element:
`<html lang="{{.lang}}" data-elysium-arrow-keys="off" data-elysium-search-key="off">`.

`data-elysium-arrow-keys="off"` stops <kbd>←</kbd> and <kbd>→</kbd> from turning
pages; `data-elysium-search-key="off"` frees <kbd>Ctrl</kbd> <kbd>K</kbd>.
