Templates
A template is HTML and CSS with a few extras. Values from data are inserted with
{{ }}, and {% %} adds logic. The syntax is Tera, close
to Jinja and Django templates.
<h1>Invoice {{ number }}</h1><p>{{ customer.name }}, issued {{ issued | date(style="long") }}</p>
<table> {% for item in items %} <tr> <td>{{ item.name }}</td> <td>{{ item.amount | currency(code="USD") }}</td> </tr> {% endfor %}</table>
{% if discount %}<p>Discount applied</p>{% endif %}
<p>Total {{ total | currency(code="USD") }}</p>Values
Section titled “Values”data is a JSON object. Reach into objects with a dot (customer.name) and loop over
arrays with {% for %}. A variable called locale is always available and holds the
locale of the render.
Values are HTML-escaped, so a customer name such as <b>Ana</b> appears as text, not as
markup. Your template is trusted markup; your data is not.
{% if x %}…{% elif y %}…{% else %}…{% endif %}and comparisons such asx > 1.{% for item in items %}withloop.index,loop.firstandloop.last.- Arithmetic:
{{ price * qty }}. {% set name = value %}to name an intermediate value.
Tera’s own filters (upper, lower, round, length and others) work too. The service
adds three for formatting, which are in Locales and formatting, one to
compare numbers, and two that draw codes.
num turns a number, a text that holds one (the amounts of your data are often text) or a
missing or blank value into a number, so that it can be compared. A text that is not a number is an
error, and not a guess. A field that is missing from the data is an error when a filter is
applied to it, so give it a default first.
{% if total | default(value=0) | num > 1000 %}Thank you for the large order.{% endif %}qr draws a QR code and barcode draws a Code 128 barcode, from any text or number of your
data. They are written with safe, as the markup is the service’s own.
{{ pay_url | qr(size=30) | safe }}{{ number | barcode(width=60, height=15) | safe }}| Filter | Arguments | Holds |
|---|---|---|
qr |
size in millimetres (5 to 200, 30 by default), level of error correction L, M, Q or H (M), margin in squares of quiet zone (0 to 10, 4) |
Up to 1000 bytes of text. |
barcode |
width and height in millimetres (20 to 200 and 5 to 100; 60 and 15), margin in modules of quiet zone (0 to 20, 10) |
Up to 80 letters, digits and punctuation, without accents. |
A code is a set of vector rectangles, so it stays sharp at any size. A missing or empty value draws nothing; a value that cannot be held is an error. All the codes of one document together are limited to 4 MiB of markup, which is far more than a page needs.
What templates cannot do
Section titled “What templates cannot do”Templates cannot include other files or read anything outside the render. The output of one render is capped by your plan, and a template that produces too much is refused.
Saving templates
Section titled “Saving templates”Create and edit templates in the portal, which previews and validates as you type, or through the API. A template must compile to be saved.
curl -X POST https://api.rustedf.com/v1/templates \ -H "Authorization: Bearer $RUSTEDF_KEY" \ -H "Content-Type: application/json" \ -d '{ "name": "Invoice", "source": "<h1>{{ number }}</h1>", "locale": "en-US", "sample_data": { "number": "2025-0042" } }'sample_data is only for the portal’s preview. Each change raises the template’s
version. Templates belong to one account: another account’s template id is a 404.
Limits on data
Section titled “Limits on data”data must be a JSON object, at most 32 levels deep and 200,000 values, and no larger than
your plan’s input limit.