Functions
TemplateTo provides special functions that extend Liquid's capabilities for document generation.
Quick Reference
| Function | Purpose | Example |
|---|---|---|
repeat() |
Iterate with JSONPath | {{ repeat("$.items[*]", "...", "...") }} |
contentBlock() |
Embed reusable blocks | {{ contentBlock("cb_123", "comp1", "All") }} |
qrCode() |
Generate QR codes | {{ qrCode(url) }} |
barcode() |
Generate barcodes | {{ barcode(code, "CODE128") }} |
isArray() |
Check if value is array | {% if isArray(items) %} |
Function Syntax
Functions use Liquid's output syntax with parentheses:
Functions can also be used in control flow:
When to Use Functions
repeat() vs for loops
Use standard {% for %} loops when:
- Iterating over a simple array
- You need full Liquid control flow
Use repeat() when:
- You need JSONPath filtering
- The array is deeply nested
- You want to filter items with JSONPath expressions
contentBlock() for reusability
Use contentBlock() when:
- Multiple templates share the same component
- You want centralized styling/content updates
- Building modular document systems
qrCode() and barcode()
Use these when: - Generating trackable documents - Creating shipping labels - Adding scannable links or references
Functions in Detail
-
repeat()
JSONPath-based iteration for advanced data filtering
-
contentBlock()
Embed reusable content blocks with data scoping
-
qrCode() & barcode()
Generate QR codes and barcodes inline
-
Utilities
Helper functions like isArray()
Combining Functions
Functions can be combined with standard Liquid:
{% if showQRCode %}
{{ qrCode(trackingUrl, "#000", "#fff", 10, 150, 150) }}
{% endif %}
{% if isArray(order.lines) %}
{{ repeat("$.order.lines[*]", "<tr><td>{{name}}</td></tr>", "<table>_</table>") }}
{% else %}
<p>Single item: {{ order.lines.name }}</p>
{% endif %}
See Also
- Liquid Reference - Standard Liquid syntax
- Filters & Tags - TemplateTo filters and tags
- Data Binding - Working with JSON data