Handlebars Reference
The Handlebars template engine is used within the tSM Output Management system to render dynamic documents based on JSON input data. This document serves as a complete reference for all custom and built-in Handlebars helpers provided in the system.
How Handlebars Works
Handlebars is a templating engine that allows you to use placeholders in the form of {{property}} to represent data fields. The engine supports the use of helpers—custom functions that provide additional logic and operations within templates.
Below is a comprehensive list of the helpers available in tSM, including both standard built-in helpers and custom helpers.
Helper Categories
1. Standard Built-In Helpers
Handlebars provides a set of built-in helpers for basic logic, iteration, and variable management:
- if: Executes a block if the condition is
true.
The else statement is optional and allows you to define a secondary block for when the condition is false.
- unless: Executes a block if the condition is
false.
This is the inverse of the if helper.
- each: Iterates over a list or array and renders the block for each item.
In this example, items is an array, and this refers to each item in the array.
- with: Changes the context of the block to the specified value.
The with helper changes the scope to the person object, allowing direct access to its properties.
2. Logical Helpers
- or: Returns
trueif at least one of the arguments istrue.
- and: Returns
trueif both arguments aretrue.
- not: Negates the given boolean argument, returning
trueif the input isfalse.
3. Comparison Helpers
- eq: Checks if two values are equal.
-
ifEquals: Alias for
eqthat performs the same equality check. -
lt: Returns
trueif the first number is less than the second.
- gt: Returns
trueif the first number is greater than the second.
-
lte: Returns
trueif the first number is less than or equal to the second. -
gte: Returns
trueif the first number is greater than or equal to the second.
4. Variable Management
- setVar: Sets a new variable in the Handlebars context.
Output: Hello, World!
5. String Manipulation Helpers
- replace: Replaces a specified substring in a string.
Output: Hello, Handlebars!
- replaceRegex: Replaces substrings matching a regular expression.
Output: Hello, World!
- first: Retrieves the first element of a comma-separated list.
Output: apple
- in: Checks if a substring is present in a given string.
6. Date and Time Helpers
- formatDate: Formats a date or timestamp to a specified pattern.
Output: 2024-10-04
- durration: Calculates the duration between two date strings in a specified unit (e.g.,
YEARS,DAYS,HOURS).
Output: 3
- secondsTime: Converts a time in seconds into
HH:mm:ssformat.
Output: 01:01:01
- parseDate: Parses a date string and extracts a specific unit (e.g., year, month).
Output: 10
- calculateDate: Adds or subtracts a time unit from a date.
Output: 2024-10-14
7. Math Helpers
- math: Performs basic arithmetic operations (addition, subtraction, multiplication, and division).
8. Output Formatting Helpers
- markdown: Converts Markdown content to HTML.
Output: <strong>Bold text</strong>
- plaintext: Converts HTML content to plain text.
Output: Hello
- parseNumberFromString: Converts a formatted number string into a numeric value.
Output: 1234.56
9. tSM Helpers
These helpers interact with other modules of the tSM system, providing additional functionality specific to tSM.
- formatCurrency: Formats a numeric value into a currency string according to locale settings.
Output: 2,500.50 Kč
- formatPrice: Formats a number to two decimal places and removes currency symbols.
Output: 2500.50
- qrCode: Generates a QR code based on the provided data and optional payment information.
- checkBoxText: Renders a formatted HTML checkbox text.
Output: <div class="fake-checkbox"></div> Agree to terms?
- button: Generates an HTML button with a specified link and label.
Output: <a href='https://example.com/action' class='button'>Click Me</a>
- userById: Retrieves the user name based on a user ID or code.
Output: John Doe
- userGroupById: Retrieves the name of a user group based on its ID or code.
Output: Administrators
10. Removing newline
When generating conditional output using Handlebars ({{#if}} blocks), it's common to encounter unwanted formatting issues such as:
- Empty lines in the output (especially when values are missing),
- Commas ending up on a separate line from the value.
To avoid this, use whitespace control (~) combined with inline conditional logic to produce compact and valid output.
Recommended pattern:
Output Example:
//Unoptimized example:
{
"myVar":"My line 1",
"myVar2":"My line 2"
}
// Optimized version using ~:
{
"myVar":"My line 1",
"myVar2":"My line 2"
}
11. Using SpEL for complex expressions
tSM Output Management supports SpEL (Spring Expression Language) to evaluate more complex conditions and expressions in templates.
SpEL can be used inside Handlebars templates using the spel helper function. This gives you more expressive power compared to standard Handlebars logic.
Handlebars vs. SpEL: Comparison example
Suppose you want to check if a variable (myVar1) matches one of several specific values (e.g. "A", "B", "C").
1. Handlebars only version:
This works, but gets hard to read as conditions grow.
2. Using spel expression:
Where you can use spel
Inside {{#if (spel "...")}} conditions
Inside {{#unless}}, {{#each}}, or custom helpers (if supported)
Tip: SpEL is evaluated at runtime inside the tSM rendering engine. It gives you access to full logical operations, null safety, type casting, math, string functions, etc.
Using Helpers in Output Management
In the context of tSM Output Management, these helpers can be leveraged to build complex document templates, providing dynamic content, logic, and formatting.