Pandoc Partial Templates ======================== Most people know about [Pandoc](https://pandoc.org/) from its fantastic ability to convert various markup formats from one to another. A little less obvious is Pandoc can be a template engine for rendering static websites allowing you full control over the rendered content. The main Pandoc documentation of the template engine can be found in the [User Guide](https://pandoc.org/MANUAL.html#templates). The documentation is complete in terms of describing the template capabilities but lacks a tutorial for using as a replacement for more ambitious rendering systems like [Jekyll](https://jekyllrb.com/) or [Hugo](https://gohugo.io/). Pandoc takes a vary direct approach and can be deceptively simple to implement. Use your own template --------------------- First thing in this tutorial is to use our own template with Pandoc when rendering a single webpage. You use the `–-template` option to provide your a template name. I think of this as the page level template. This template, as I will show later, can then call other partial templates as needed. Example, render the [Pandoc-Partials.txt](Pandoc-Partials.txt) file using the template named [index1.tmpl](index1.tmpl): ~~~{.shell} pandoc --from=markdown --to=html \ --template=index1.tmpl Pandoc-Partials.txt > index1.htm ~~~ This is a simple template page level template. ~~~{.html-code}
${body} ~~~ When we run our Pandoc command the file called [Pandoc-Partials.txt](Pandoc-Partials.txt) is passed into the template as the "body" element where it says `${body}`. See this Pandoc [User Guide](https://pandoc.org/MANUAL.html#templates) for the basics. Example 1 rendered: [index1.htm](index1.htm) Variables and metadata ---------------------- Pandoc's documentation is good at describing the ways of referencing a variable or using the built-in template functions. Where do the variables get their values? The easiest way I've found is to set the variables values in a JSON metadata file. While Pandoc can also use the metadata described in YAML front matter Pandoc doesn't support some of the other common front matter formats. If you're using another format like JSON or TOML for front matter there are tools which can split the front matter from the rest of the markdown document. For this example I have created the metadata as JSON in a file called [metadata.json](metadata.json). Example [metadata.json](metadata.json): ~~~{.json} { "title": "Pandoc Partial Examples", "nav": [ {"label": "Pandoc-Partials", "href": "Pandoc-Partials.html" }, {"label": "Version 1", "href": "index1.htm" }, {"label": "Version 2", "href": "index2.htm" }, {"label": "Version 3", "href": "index3.htm" } ] } ~~~ Let's modify our initial template to include our simple navigation and title. Example [index2.tmpl](index2.tmpl): ~~~{.html-code} ${if(title)}