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

3.1 KiB

title description
Templating Changing the Iocaine template

iocaine uses Handlebars for templating, and uses only a single template, main. See the Handlebars for basic syntax. A default template is provided, but if you want to change it, you can configure a template directory, and place a customized main.hbs file in it, and iocane will use that over the default.

Available variables

Each time a page is rendered, iocaine makes data in the following shape available:

{
  "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:

is-root

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.

markov-gen GROUP INDEX WORD_COUNT

Generates exactly WORDS_MAX words of garbage from the markov sources, for the INDEXth paragraph, using the GROUP seed group. Word counts for the paragraphs are pre-generated, and available via paragraphs. Example usage:

{{#each paragraphs}}
  <p>{{ markov-gen "garbage" this.index this.value }}</p>
{{/each}}
href-gen GROUP INDEX WORD_COUNT

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, for the INDEXth link, using the GROUP seed group. Word counts for hrefs and titles are available via the links property. Example usage:

<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>

Putting it all together

With the pre-generated data, and the provided functions, we can construct a customized template:

<!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>