Direkt zum Hauptinhalt

Template Syntax

Templates are mostly used to create an HTML version of your content, which then can be used to create further formats (e.g., PDF, EPUB). Our templates are based on Handlebars, a simple template engine.

Print a value

To display a value (see available fields) you can use two curly brackets:

<h1>Book Title: {{metadata.title}}</h1>

You will need to use three curly brackets, if your fields contain HTML:

<h1>Book Title with HTML: {{{metadata.title}}}</h1>

Conditions

You may want to hide/show fields depending on if they are empty/unset or not.

If-Condition

Use the if condition to only do something, if the value is not null/empty:

{{#if metadata.subtitle}}
<h2>{{metadata.subtitle}}</h2>
{{/if}}

This code snippet will only create a heading if metadata.subtitle is not null/empty.

Why should I bother using if's?
You could just use {{metadata.subtitle}} and if it's empty it will not show. But it will typically still affect spacing/etc. if you create the HTML element (e.g., an empty <h2></h2>)

You can also add an else branch, which will be executed if the specified field is null/empty:

{{#if metadata.title}}
<h1>{{metadata.title}}</h1>
{{else}}
<h1>Lorem Ipsum</h1>
{{/if}}

This code snippet will print "Lorem Ipsum"Ipsum” if metadata.title is null/empty.

Unless-Condition

If you want to show something, unless a variable is set & not emptyempty, you can use unless:

{{#unless metadata.title}}
<h1>Warning: Title missing!</h1>
{{/unless}}

Iterate through multiple Entries

Often you will need to iterate through a list of things, e.g. authors or sections. You can use each for that:

<h1>Contributing Authors:</h1>

{{#each metadata.authors}}
<p>{{first_names}} {{last_names}}</p>
{{/each}}

This snippet will show first and last names of every author in the project.

Context Change
Note that the context changes with the use of each: You may expectedexpect we would use {{metadata.authors.first_names}}, but we have to strip away metadata.authors since we "entered" the context when we used each.