Templates

Templates use Jinja2 syntax: variables, if, for, filters, macros, include, import, extends.

Jinja runs first

For each page, Merodi first treats the whole Markdown file as a Jinja template. The text that comes out is then converted from Markdown to HTML. So Jinja can generate Markdown or HTML, and your templates are plain Markdown or HTML fragments, not complete HTML layouts.

{% for name in ["one", "two", "three"] %}
- {{ name }}
{% endfor %}

That produces a three-item list. The Markdown step then sees an ordinary list.

Variables

Set a variable in the page with set:

{% set page_title = "About" %}

Or set one from a plugin so every page can use it:

merodi.jinja.set("site_name", "My Site")
merodi.jinja.set("links", { { title = "Home", url = "index.html" } })
{{ site_name }}
{% for l in links %}[{{ l.title }}]({{ l.url }}) {% endfor %}

Strings, numbers, booleans and Lua tables work. A Lua list becomes a Jinja list and any other table becomes a mapping.

The templates folder

Files in the templates folder are found by name from include, import and extends.

{% include "header.md" %}

An included file sees the variables set before the include. That is how this site passes the page title to its shared head.

Macros work the same way. In templates/macros.md:

{% macro note(text) %}> **Note:** {{ text }}{% endmacro %}

In a page:

{% import "macros.md" as m %}
{{ m.note("Careful.") }}

Document tags

Merodi adds four tags for the document head.

Tag Effect
{% document title="..." lang="..." %} Make the page a complete HTML document
{% meta name="..." content="..." %} Add a <meta name content> to the head
{% style "path.css" %} Add a <link rel="stylesheet">
{% script "path.js" %} Add a <script defer>

Arguments can be strings or variables: {% document title=page_title %}.

A page that uses {% document %} is written like this:

<!DOCTYPE html>
<html lang="en">
<head>
<title>...</title>
<meta name="..." content="...">
<link rel="stylesheet" href="...">
<script src="..." defer></script>
</head>
<body>
... the page ...
</body>
</html>

Details:

The document variable

While a page renders, the variable document holds what the tags have set so far:

{{ document.Title }} / {{ document.Lang }}

Showing Jinja code

To print template syntax as text, wrap it in raw:

{% raw %}
{% document title="x" %}
{% endraw %}

Errors

Template errors stop the build and name the line:

[ERROR]: Jinja syntax error:
- Line=1, Column=4
- ControlStructure 'bogus' not found (or beginning not provided)