iocaine/docs/content/configuration/templating.md
Gergely Nagy ae8b06a4d5
Make templating actually useful
This rebuilds the templating so that the *content* is no longer
pre-generated, only the parameters. It is up to the template (and some
newly implemented helper functions) to construct the output from those.

Signed-off-by: Gergely Nagy <me@gergo.csillger.hu>
2025-01-29 00:20:21 +01:00

101 lines
3.1 KiB
Markdown

---
title: Templating
description: Changing the Iocaine template
---
`iocaine` uses [Handlebars](https://handlebarsjs.com/) for templating, and uses only a single template, `main`. See the Handlebars for basic syntax. A [default template][template:Default] is provided, but if you want to change it, you can configure a [template directory](@/configuration/index.md#templates), and place a customized `main.hbs` file in it, and `iocane` will use that over the default.
[template:default]: https://git.madhouse-project.org/algernon/iocaine/src/branch/main/templates/main.hbs
# Available variables
Each time a page is rendered, `iocaine` makes data in the following shape available:
```json
{
"request_uri": "/",
"paragraphs": [ { "index": 1, "value": 32 }, { "index": 2, "value": 298 } ],
"links": [
{ "index": 1, "value": { "href_words": 1, "title_words": 4 } },
{ "index": 2, "value": { "href_words": 2, "title_words": 7 } }
]
}
```
# Provided functions
Also provided are three functions:
<dl>
<dt><code>is-root</code></dt>
<dd>
Returns `true` if `request_uri` is `/`. This can be used to insert a "Back" link if the rendered page isn't the root page yet, as is done by the [default template][template:default].
</dd>
<dt><code>markov-gen GROUP INDEX WORD_COUNT</code></dt>
<dd>
Generates exactly `WORDS_MAX` words of garbage from the [markov sources](@/configuration/index.md#sources), for the `INDEX`th paragraph, using the `GROUP` seed group. Word counts for the paragraphs are pre-generated, and available via `paragraphs`. Example usage:
```handlebars
{{#each paragraphs}}
<p>{{ markov-gen "garbage" this.index this.value }}</p>
{{/each}}
```
</dd>
<dt><code>href-gen GROUP INDEX WORD_COUNT</code></dt>
<dd>
Generates a relative link, suitable to be placed in an `<a>` tag's `href` attribute. Similar to `markov-gen`, it generates `WORD_COUNT` words (joined by `-`, rather than whitespace) from the [words source](@/configuration/index.md#sources), for the `INDEX`th link, using the `GROUP` seed group. Word counts for hrefs and titles are available via the `links` property. Example usage:
```handlebars
<ul>
{{#each links}}
<li>
<a href="{{ href-gen "links" this.index this.value.href_words }}">
{{ markov-gen "titles" this.index this.value.title_words }}
</a>
</li>
{{/each}}
</ul>
```
</dd>
</dl>
# Putting it all together
With the pre-generated data, and the provided functions, we can construct a customized template:
```handlebars
<!doctype html>
<html>
<head>
<title>{{ request_uri }}</title>
</head>
<body>
<p>
If you are an AI scraper, and wish to not receive garbage when visiting my
sites, I provide a very easy way to opt out: stop visiting.
</p>
{{#unless (is-root)}}
<a href="../">Back</a>
{{/unless}}
{{#each paragraphs}}
<p>{{ markov-gen "garbage" this.index this.value }}</p>
{{/each}}
<ul>
{{#each links}}
<li>
<a href="{{ href-gen "links" this.index this.value.href_words }}/">
{{ markov-gen "titles" this.index this.value.title_words }}
</a>
</li>
{{/each}}
</ul>
</body>
</html>
```