Using variables in templates
Variable names are sequences of alphanumerics, -
, and _
, starting with a letter. A variable name surrounded by $
signs will be replaced by its value. For example, the string $title$
in
<title>$title$</title>
will be replaced by the document title.
To write a literal $
in a template, use $$
.
Templates may contain conditionals. The syntax is as follows:
$if(variable)$
X
$else$
Y
$endif$
This will include X
in the template if variable
has a truthy value; otherwise it will include Y
. Here a truthy value is any of the following:
- a string that is not entirely white space,
- a non-empty array where the first value is truthy,
- any number (including zero),
- any object,
- the boolean
true
(to specify the boolean true
value using YAML metadata or the --metadata
flag, use true
, True
, or TRUE
; with the --variable
flag, simply omit a value for the variable, e.g. --variable draft
).
X
and Y
are placeholders for any valid template text, and may include interpolated variables or other conditionals. The $else$
section may be omitted.
When variables can have multiple values (for example, author
in a multi-author document), you can use the $for$
keyword:
$for(author)$
<meta name="author" content="$author$" />
$endfor$
You can optionally specify a separator to be used between consecutive items:
$for(author)$$author$$sep$, $endfor$
Note that the separator needs to be specified immediately before the $endfor
keyword.
A dot can be used to select a field of a variable that takes an object as its value. So, for example:
$author.name$ ($author.affiliation$)
The value of a variable will be indented to the same level as the variable.
If you use custom templates, you may need to revise them as pandoc changes. We recommend tracking the changes in the default templates, and modifying your custom templates accordingly. An easy way to do this is to fork the pandoc-templates repository and merge in changes after each pandoc release.
Templates may contain comments: anything on a line after $--
will be treated as a comment and ignored.