Docs / Template language

Templates

Template language

A template is HTML with Liquid placeholders. If you have used Shopify themes, Jekyll or Handlebars, you already know most of it.

<h1 class="doc-title">Invoice {{ number }}</h1>
<p>Billed to {{ customer.name }}</p>

<table class="items">
  <thead><tr><th>Item</th><th class="num">Amount</th></tr></thead>
  <tbody>
    <tr data-each="item in items">
      <td>{{ item.name }}</td>
      <td class="num">{{ item.amount | money }}</td>
    </tr>
  </tbody>
</table>

<p data-if="notes">{{ notes }}</p>

#Output

{{ }} prints a value from your data. Use dots to reach into objects and brackets for array positions.

{{ customer.name }}
{{ items[0].name }}
{{ items.size }}

Output is HTML-escaped, so a customer called <script> cannot break your document. To print trusted HTML as is, add the raw filter:

{{ terms_html | raw }}

A value that is missing prints nothing. It is never an error.

If the data you send is not an object (an array, for example), it is available as data.

#Loops

Put data-each on the element you want repeated. The element, with everything inside it, is rendered once per item.

<tr data-each="item in items">
  <td>{{ forloop.index }}</td>
  <td>{{ item.name }}</td>
</tr>

Inside a loop you have Liquid's forloop object: forloop.index (from 1), forloop.first, forloop.last, forloop.length.

Liquid's loop options work too:

<li data-each="tag in tags limit:5">{{ tag }}</li>
<li data-each="row in rows reversed">{{ row.label }}</li>

#Conditions

data-if renders the element only when the condition is true. data-unless is the opposite.

<p data-if="notes">{{ notes }}</p>
<span class="badge ok" data-if="status == 'paid'">Paid</span>
<span class="badge warn" data-unless="status == 'paid'">Due</span>
<div data-if="items.size > 10 and customer.vip">Thanks for the big order.</div>

Operators: ==, !=, >, <, >=, <=, contains, and, or.

Empty strings, 0, null, false and missing values all count as false, so data-if="discount" hides the row when the discount is zero.

You can combine data-each and data-if on one element. The loop runs first and the condition is checked per item, which filters the list:

<tr data-each="item in items" data-if="item.quantity > 0">...</tr>

#Why attributes

Plain Liquid tags like {% for %} work in text, but HTML does not allow text between table rows. Browsers and visual editors move or delete a bare {% for %} sitting between two <tr> elements, and the template breaks. Attributes keep the template valid HTML, so it survives any editor.

Inline Liquid tags are still fine inside text and attribute values:

<p>Payment is {% if days_late > 0 %}overdue{% else %}on time{% endif %}.</p>
<tr class="{% if item.highlight %}strong{% endif %}">

#Calculations

Put calculations in a <template data-logic> block at the top. It prints nothing; it only sets variables.

<template data-logic>
{% assign subtotal = items | line_total: "quantity", "unit_price" %}
{% assign tax = subtotal | times: tax_rate | divided_by: 100 | round: 2 %}
{% assign total = subtotal | plus: tax %}
</template>

<div class="totals">
  <div class="row sub"><span>Subtotal</span><span>{{ subtotal | money }}</span></div>
  <div class="row sub"><span>Tax ({{ tax_rate | percent }})</span><span>{{ tax | money }}</span></div>
  <div class="row grand"><span>Total</span><span>{{ total | money }}</span></div>
</div>

Let the template do the maths. Your code sends line items and the document adds them up, which removes a whole class of "the PDF total does not match" bugs.

#Formatting filters

DevilPDF adds these filters to standard Liquid.

FilterExampleOutput
money{{ 1234.5 | money }}$1,234.50
money with currency{{ 1234.5 | money: "EUR", "de-DE" }}1.234,50 €
number{{ 1234.5 | number: 2 }}1,234.50
percent{{ 13 | percent }}13%
percent with decimals{{ 7.5 | percent: 2 }}7.5%
line_total{{ items | line_total: "quantity", "unit_price" }}Sum of quantity × unit_price
sum_of{{ payments | sum_of: "amount" }}Sum of one field
initials{{ "Dana Whitfield" | initials }}DW

percent rounds to a whole number unless you pass the number of decimals to keep.

#Currency and locale

money reads two optional top-level fields from your data:

  • currency: an ISO 4217 code. Default USD.
  • locale: a BCP 47 locale. Default en-US.
{ "currency": "CAD", "locale": "fr-CA", "items": [] }

Set them once in your data and every money and number in the document follows. Pass arguments to override for one value: {{ fee | money: "USD" }}.

#Sums

line_total multiplies two fields on each item, rounds each line to cents, then adds the lines. It defaults to quantity and unit_price, so {{ items | line_total }} is enough when your items use those names. Field names can be dotted paths, such as "price.net".

#Standard filters

All of Liquid's built-in filters are available. The ones you will use most:

FilterExample
default{{ title | default: "Invoice" }}
date{{ issue_date | date: "%b %-d, %Y" }}
upcase, downcase, capitalize{{ status | upcase }}
plus, minus, times, divided_by, round{{ price | times: 1.13 | round: 2 }}
size{{ items | size }}
join{{ tags | join: ", " }}
truncate{{ description | truncate: 80 }}
where, map, sort{{ items | where: "taxable", true | size }}

An unknown filter is an error, so typos are caught when you save.

#Dates

Send dates as ISO strings ("2026-03-14" or "2026-03-14T09:30:00Z") and format them with date.

FormatOutput
%b %-d, %YMar 14, 2026
%B %-d, %YMarch 14, 2026
%Y-%m-%d2026-03-14
%d/%m/%Y14/03/2026
%-I:%M %p9:30 AM

Dates print in UTC, exactly as written. {{ "now" | date: "%Y" }} gives the current year.

#Multi-line text

Addresses and notes often contain line breaks. Put them in an element with the kit class lines and send \n in the JSON:

<div class="lines">{{ customer.address }}</div>

#Images

Use image URLs from your data, and guard optional ones so a missing logo leaves no broken image:

<img class="logo" data-if="company.logo_url" src="{{ company.logo_url }}" alt="">
<div class="brand-name" data-unless="company.logo_url">{{ company.name }}</div>

Images load from public URLs, or inline as data: URIs. Templates never run scripts.

#What is not available

  • {% include %}, {% render %}, {% layout %} and {% block %}. A template is one self-contained document.
  • JavaScript. For documents that need scripts, such as charts, render raw HTML with options.javascript.