Guide

Data binding

Put values from your JSON into a PDF template with Handlebars tags, repeat rows for lists, and show parts only when a field is set.

On this page

Community

Brevier binds data with Handlebars tags. It supports the parts of Handlebars that a document needs, and it is stricter: a field that does not exist is an error, not an empty space.

Values

{{ path }} prints a value. The path uses dots to go into objects.

data.json
{ "kunde": { "name": "Erika Mustermann", "ort": "50667 Köln" } }
<p>{{ kunde.name }}, {{ kunde.ort }}</p>

Brevier escapes every value. A < in your data prints as <, and it never becomes HTML.

If a field is missing, the render stops. The error names the field and the template line:

Error: Missing field 'kunde.strasse' at line 2

A name with a space or a dash goes in square brackets: {{ [Rechnungs-Nr.] }}, {{ kunde.[first name] }}.

{{! note }} is a comment. It is not printed. Use {{!-- note --}} when the note itself contains }}.

Formatters

A formatter changes how a value prints. Write it after a |.

<td>{{ betrag | money }}</td>

The formatter reference lists all 12 formatters and the totals, such as sum, with an example for each.

Lists

{{#each list}} repeats its content once for every item. Inside the loop, a path first looks in the current item, then in the whole data.

<tbody>
  {{#each rechnung.positionen}}
  <tr><td>{{ @index }}</td><td>{{ text }}</td><td>{{ betrag | money }}</td></tr>
  {{/each}}
</tbody>

Names such as {{ this }}, {{ @index }}, {{ ../name }} and {{ @root.name }} also work inside the loop. The template syntax reference lists them all.

Add {{else}} for an empty list:

{{#each rechnung.positionen}}
  <p>{{ text }}</p>
{{else}}
  <p>No items.</p>
{{/each}}

Conditions

{{#if path}} shows its content only when the value is set. {{#unless path}} shows it only when the value is not set.

{{#if kunde.firma}}
  <p>{{ kunde.firma }}</p>
{{else if kunde.name}}
  <p>{{ kunde.name }}</p>
{{else}}
  <p>Private customer</p>
{{/if}}

{{#unless rechnung.bezahlt}}<p>Open</p>{{/unless}}

The template syntax reference lists which values count as not set.

A quantity of 0 is often a real value. Write includeZero=true to show it:

{{#if menge includeZero=true}}<td>{{ menge }}</td>{{/if}}

Short names for an object

{{#with object}} makes the fields of one object available by their short names. {{else}} runs when the object is not set.

{{#with kunde}}
  <p>{{ name }}, {{ ort }}</p>
{{else}}
  <p>No customer.</p>
{{/with}}

What Brevier does not support

Handlebars feature Use instead
{{{ html }}} Raw output is not available in a template yet.
Helpers and subexpressions Formatters, like {{ preis | money }}.
Partials Not supported.