Handlebars.java Helpers
Handlebars.java provides built-in helpers that extend the templating capabilities, making HTML templates dynamic and easier to manage. SØAD Framework also includes custom helpers to further enhance your templates. This chapter explains the most common helpers and custom SØAD-specific helpers available.
Helper Quick Reference
| Helper | Category | What it does |
|---|---|---|
if, unless |
Built-in | Conditional blocks that render or skip markup based on a truthy value. |
each |
Built-in | Iterates over collections and renders children for every item. |
eq, neq |
Logical | Compare two values for equality or inequality inside inline conditions. |
gt, gte, lt, lte |
Logical | Evaluate greater-than and less-than comparisons without writing Java code. |
and, or, not |
Logical | Combine boolean expressions to keep template logic concise. |
ref_lookup |
Data | Fetch a labeled value from a referenced table or object using a key. |
select, option |
Form | Generate dropdown elements populated from tables, collections, or filters. |
dateFmt |
Formatting | Render dates using any java.time-style format pattern. |
in |
Logical | Check whether a value exists in an array, list, or delimited string. |
html |
Formatting | Sanitize rich text, expose basic formatting, and auto-link URLs. |
session |
Context | Read an attribute directly from the current HTTP session. |
get |
Context | Access keys in ctx.output that contain spaces or special characters. |
i18n |
Localization | Resolve a translation key from the active locale resource bundle. |
Built-in Handlebars Helpers
Below are basic built-in Handlebars helpers commonly used:
1. Conditional Helpers
- if: Conditionally renders content.
- unless: Inverse of
if.
2. Iteration Helper
- each: Iterates over a collection.
3. Logical Helpers
- eq: Checks equality.
For more information on the Handlebars templating language and available features, visit the official documentation at https://jknack.github.io/handlebars.java/.
SØAD Custom Helpers
SØAD provides additional custom helpers to simplify common tasks:
1. ref_lookup
Lookup a value from a database table using a given key.
Parameters:
table: The name of the database table to query.refs: The object containing the key to look up. Iftableis not specified, thenrefsmust be provided.label: The column to retrieve from the table. Defaults toname. For more than one column, use a pipe-separated list (e.g.,label="name|email").value: The column to match against the provided key. Defaults toid. This column is typically the primary key of the table.
Example Usage
This will look up the login_id for the user with the specified id in the user table.
This iterates over a list of users and retrieves the full_name from the user_details table for each user.
2. select and option
Generate dropdown menus (<select>) and their options.
Parameters:
table: The name of the database table to query.refs: The object containing the key to look up. Iftableis not specified, thenrefsmust be provided.filter: Optional filter to apply to the query. e.g.,filter="active=1".id: The ID of the<select>element.name: The name of the<select>element.class: CSS class for the<select>element.label: The column to display as the option text. Defaults toname.value: The column to match against the provided key. Defaults toid.selected: The value to pre-select in the dropdown.required: If set totrue, the dropdown will be required.readonly: If set, the tag will turn to label.sel_text: Text to display when no option is selected. Defaults to "Please Select".
Generate <option> elements based on the specified table or object.
Uses the same parameters as select, but generates individual <option> elements instead of a full <select>. This is useful when you need to customize the <select> tag.
Example Usage
3. dateFmt
Format a date object into a human-readable format.
Formats the registration_date into a day/month/year format. If no format is specified, it defaults to dd/MM/yyyy.
4. in
Check if a value exists within a list or a string.
5. html
Sanitize HTML content, convert newlines to <br> and generate anchor for links.
6. session
Access session attributes.
7. get
Retrieve values from ctx.output by keys containing special characters or spaces.
8. i18n
Internationalization helper to fetch localized strings. This helper retrieves strings from the i18n resource files (messages.properties) based on the current locale or __locale__ value from session attributes. To use this helper, you need to have a messages.properties file in your resources directory with the appropriate key-value pairs.
Properties files example:
To set the locale, you can use the __locale__ session attribute:
Refer to the Multi-language Support section for more details on how to set up and use i18n in SØAD.