Home > database >  How can I include a template in another with pandoc?
How can I include a template in another with pandoc?

Time:04-15

I am using Markdown files to create a series of webpages (calling that mess a website would probably be frowned upon). I use a general template to display each page with a similar look: they all have the same header, footer, etc.

I would like to add a navigation menu, in order to have links to the other pages. I can easily generate the menu itself, what I don't know is how to insert it in the page.

What I tried is the following:

options.yml (generated by a Python script):

metadata:
  title: My very excellent title
  navigation: <a href="index.html">Home</a><a href="other.html">Other page</a>
standalone: true
template: template.html

template.html:

<!doctype HTML>

<title>$title$</title>

<header>Yeepee, header!</header>

<nav>$navigation$</nav>

<main>$body$</main>

<footer>Best footah evah</footer>

I then run the script: pandoc -d options.yml index.md -o index.html (and same for the other files, in a loop in a Python script)

The result is that the content of my metadata.navigation is escaped before insertion, resulting in something like &lt;a href="index.html"&gt;Home&lt;/a&gt;&lt;a href="other.html"&gt;Other page&lt;/a&gt;, which is really safe in practice, but doesn't help me there.

What I would like is to have another template, say navigation.html, that contains the navigation menu to be included in my main template when using pandoc.

If this is not possible, I would like to use the same technique as above, but with an "unescaped" navigation parameter (I'm not fond of it, as it would bring a major security issue into the project).

How can I achieve this?

CodePudding user response:

There are two solutions to this:

  1. Use variables instead of metadata in your defaults file. Variables are inserted verbatim, while metadata will be escaped.

  2. To insert the file navigation.html in a template, use ${navigation.html()}. Pandoc uses the doctemplates package for templates, see the docs on "partials" for more details.

  • Related