#+TITLE: Templates #+DESCRIPTION: Layouts, inheritance, every variable and filter available to a template. #+LEDE: minijinja layouts loaded from disk, hashed into the cache, replaceable entirely. Templates live in the directory named by =[templates] dir=, default =templates/=. They are [[https://docs.rs/minijinja][minijinja]] templates — Jinja2 syntax — loaded at build time, so editing one and rebuilding is the whole edit cycle. * base.html replaces the layout A file called =base.html= becomes the page layout. Without one, a built-in layout is used — which is what makes a bare directory of org files build into a real site. #+BEGIN_SRC html
Only this part differs.
{% endblock %} #+END_SRC Subdirectories work, so ={% include "partials/header.html" %}= does what you expect. ** Partials keep a snippet out of a layout A block of content that is not really layout — an invitation to reply, a licence line, a donation ask — is better as its own file than as a line inside =base.html=: #+BEGIN_SRC html {% extends "base.html" %} {% block content %} {{ body | safe }} {% include "reply.html" %} {% endblock %} #+END_SRC Emptying =reply.html= removes it everywhere; editing it re-renders exactly the pages that include it, because the render key follows includes. And because the *page* chooses its layout, an individual page opts in with =#+TEMPLATE: post.html= or out with =#+TEMPLATE: base.html=, without a rule deciding for a whole directory. * Variables ** body The rendered page HTML. Always use ={{ body | safe }}= — it is already HTML, and escaping it would print tags at the reader. Empty on generated pages, which build their content from =pages= or =groups= instead. ** page | Field | Meaning | |-------+---------| | =title= | =#+TITLE:=, or the filename stem. | | =url= | Output path relative to the site root, e.g. =blog/post.html=. | | =source= | Source path relative to the source root, e.g. =blog/post.org=. | | =date= | =#+DATE:= verbatim, in whatever org syntax was written. | | =date_iso= | The =YYYY-MM-DD= inside it, or =none=. | | =year= | The year from that date, for grouping a listing. | | =tags= | =#+FILETAGS:=, split. | | =excerpt= | =#+DESCRIPTION:=, or the first paragraph. | | =word_count= | Words of prose, excluding code blocks. | | =reading_time= | Minutes at 200 wpm, rounded up. | | =toc= | The heading tree. See below. | | =keywords= | *Every* =#+KEYWORD:=, by lowercased name. | =page.keywords= is the escape hatch: =#+CUSTOM_THING: x= is ={{ page.keywords.custom_thing }}=, so your own metadata works without orgo knowing it exists. ** site =site.title=, =site.base_url=, =site.description=, =site.language= — straight from =[site]= in the config. ** nav A list of ={title, url}=, with URLs relative to the current page. ** root The =../= prefix back to the site root from this page: empty at the top level, =../= one level down. Prefix it to any site-root-relative path so the same template works at any depth: #+BEGIN_SRC html {{ post.title }} #+END_SRC ** stylesheet URL of the generated =syntax.css=, relative to this page. Link it or code blocks are unstyled. ** theme URL of =theme.css=, relative to this page — the [[file:02-configuration.org][built-in theme]] named by =site.theme=. Empty when there is none, which is the default, so guard it and link it *before* =stylesheet= or the theme's code colours would override the highlighter's: #+BEGIN_SRC html {% if theme %}{% endif %} {% if stylesheet %}{% endif %} #+END_SRC A layout that ignores it is a layout with its own CSS, which is the point at which you have outgrown the setting. ** pages, group, groups, paginator Present on generated pages; see [[file:03-collections.org][Collections]]. =pages= is also available on every page when =[templates] expose_page_list = true=. ** page.toc The page's headings as a *tree* — a table of contents is one, and rebuilding a tree from a flat list of levels inside a template is what Jinja is worst at. Each entry has =title=, =anchor=, =level=, =number= and =children=: #+BEGIN_SRC html {% macro toc_list(entries) %}