How to PDF Template Variables

Template variables let you inject dynamic data into PDF templates without modifying the HTML itself — customer names, order totals, dates, and any other field that changes per document. SublimePDF uses Handlebars syntax for variable interpolation, giving you expressions, loops, conditionals, and helpers in a familiar, logic-light template language.

Follow the step-by-step instructions below, then use the free tool directly — no registration or download required.

Open Tool →

How to PDF Template Variables — Step by Step

1

Define variables in your template

Insert {{ variableName }} anywhere in your HTML template where dynamic content should appear. For nested objects, use dot notation: {{ customer.address.city }}. Variables work in text nodes, attribute values, and even CSS (though CSS usage is less common).

2

Pass data in the API request

Include a 'data' object in your render request: { "templateId": "tmpl_abc", "data": { "customerName": "Jane Doe", "invoiceTotal": "$1,250.00" } }. Each key in the data object maps to a template variable.

3

Use loops for repeating content

Wrap repeating sections in {{#each arrayName}}...{{/each}}. Inside the block, {{ this.property }} accesses each item's fields. Use {{@index}} for zero-based iteration count and {{@first}} / {{@last}} for boundary detection.

4

Add conditionals for optional sections

Use {{#if showDiscount}}...{{/if}} to conditionally include content. {{#unless isPaid}}Payment Pending{{/unless}} inverts the condition. For equality checks, register a custom helper or use {{#ifEquals status 'completed'}} with a built-in SublimePDF helper.

5

Format data with built-in helpers

SublimePDF includes built-in Handlebars helpers: {{formatCurrency amount}} for locale-aware currency formatting, {{formatDate date 'MMMM D, YYYY'}} for date formatting, and {{uppercase text}} for text transformation. Pass the helper name followed by the variable.

Pro Tips

  • 💡 Escape HTML in user-provided data by default — Handlebars auto-escapes {{ }} output. Use triple braces {{{ rawHtml }}} only when you intentionally want to inject unescaped HTML content.
  • 💡 Use {{> partialName}} to include reusable template snippets (headers, footers, address blocks) stored as partials in your template library.
  • 💡 Set default values for optional variables using the 'defaults' parameter in your render request — this prevents empty spots when a data field is missing.
  • 💡 Variables inside CSS class attributes work well for conditional styling: <div class="status-badge {{ statusClass }}"> dynamically applies different CSS classes based on your data.

Privacy & Security

All processing happens directly in your browser. Your files are never uploaded to any server — they remain on your device throughout the entire process. SublimePDF uses WebAssembly technology for fast, secure, client-side processing.

Works Everywhere

This tool works on any modern browser — Chrome, Firefox, Safari, or Edge — on desktop, tablet, or mobile. No software to install. PDF is an open ISO standard supported by all major platforms.

How to PDF Template Variables — FAQ

What templating engine does SublimePDF use?
Handlebars.js — a widely-used, logic-light templating language. It supports variables, loops (#each), conditionals (#if), partials, and custom helpers. It intentionally limits complex logic to keep templates readable and maintainable.
Can I perform calculations in templates?
Handlebars discourages in-template math. Perform calculations in your backend code and pass the results as variables. For simple cases, use the built-in {{math}} helper: {{math subtotal '*' taxRate}} — but complex formulas belong in your application logic.
How do I handle missing or null variables?
Missing variables render as empty strings by default. Use {{#if variableName}} guards to show fallback content. Enable 'strictMode': true in the API request to receive an error when any referenced variable is missing from the data payload.
Can I use variables in the header and footer templates?
Header and footer templates don't receive your data object directly. To include dynamic content in headers/footers, pre-render those values into the header/footer template strings before passing them to the API.

Ready to get started?

Use SublimePDF's free tools right now.

Open Tool