Custom Handlebars.js helpers
How to add custom certificate helpers
What Custom Helpers Are
{{formatNationalId ($lookup $declaration "applicant.nationalId")}}How They Work End-to-End
Country config server starts
→ handler.ts reads helpers.ts
→ esbuild compiles TypeScript → JavaScript (ESM)
→ compiled JS is served at GET /handlebars.js
Client loads the app
→ referenceApi.importHandlebarHelpers() fetches /handlebars.js
→ all named exports are stored in memory as the helpers registry
User prints or reviews a certificate
→ compileSvg() is called
→ for each helper name in the registry:
customHelpers[helperName]({ intl }) ← factory called with intl
Handlebars.registerHelper(name, result)
→ Handlebars.compile(svgTemplate)(data) runs
→ custom helper is called wherever {{helperName ...}} appears in SVGWhere to Write Them
The Factory Pattern
How Many Parameters You Can Pass
The options Object
options Objectoptions.fn and options.inverse
options.fn and options.inverseoptions.hash
options.hashoptions.data.root
options.data.rootKey
What it is
options.data iteration keys
options.data iteration keysExample of custom handlebar helpers
1. Simple Helper (no intl)
2. Helper with i18n translation
3. Helper with multiple arguments
4. Debug helper — inspect template context
5. Accessing all template variables via options.data.root
options.data.root6. Block helpers with options.fn and options.inverse
options.fn and options.inverse7. Named hash parameters
8. Generating SVG tspan elements
Using Custom Helpers in SVG Templates
TypeScript Imports
Limitations
Constraint
Detail
Real-World Examples
Age calculation from two dates
Nationality / country code translation with locale override
Ternary — conditional value inline
First non-empty value
Localized gender string
Generate a multi-line certificate paragraph
Summary
Task
Pattern
Last updated