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
documenttag is what turns wrapping on.{% document %}with no arguments is enough. Without it, the page is written as bare HTML andmeta,styleandscripttags are ignored. titledefaults to thedescriptioninconfig.toml.langdefaults toen.metatakes onlynameandcontent. It cannot write<meta charset>. The cookbook shows how to add one with a plugin.styleandscriptcan appear many times. They are written in the order they appear.- The tags also work inside an included template, so a shared
head.mdcan hold them all. - Paths are written as given. Use paths relative to the page, such as
static/style.css, if you want the site to work from any folder.
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)