Template Helpers
oJo provides a set of built-in Handlebars helpers to enhance your template functionality. These helpers can be used directly in your templates without any additional configuration.
Data Manipulation
JSON Conversion
- json: Converts a value to a JSON string
{{json object}}- array: Converts an array to a string with brackets
{{array [1, 2, 3]}}String Operations
- concat: Concatenates multiple strings
{{concat 'Hello' ' ' 'World'}} <!-- Output: Hello World -->- lowercase: Converts string to lowercase
{{lowercase 'HELLO'}} <!-- Output: hello -->- uppercase: Converts string to uppercase
{{uppercase 'hello'}} <!-- Output: HELLO -->- capitalize: Capitalizes first letter of string
{{capitalize 'hello'}} <!-- Output: Hello -->- substring: Extracts part of a string
{{substring 'Hello World' 0 5}} <!-- Output: Hello -->Numeric Operations
Math Operations
- math: Performs basic mathematical operations
{{math value1 '+' value2}}
<!-- Addition -->
{{math value1 '-' value2}}
<!-- Subtraction -->
{{math value1 '*' value2}}
<!-- Multiplication -->
{{math value1 '/' value2}}
<!-- Division -->
{{math value1 '%' value2}}
<!-- Modulo -->Number Manipulation
- round: Rounds a number to specified decimals
{{round 3.14159 2}} <!-- Output: 3.14 -->- inc: Increments a number by 1
{{inc 5}} <!-- Output: 6 -->- dec: Decrements a number by 1
{{dec 5}} <!-- Output: 4 -->Number Formatting
- formatNumber: Formats a number with commas
{{formatNumber 1000}} <!-- Output: 1,000 -->- formatCurrency: Formats a number as currency
{{formatCurrency 1000 'USD'}} <!-- Output: $1,000.00 -->- formatCurrency: Formats a number as currency with options: minDigits, maxDigits
{{formatCurrency 1000 'USD' 0 1}} <!-- Output: $1,000 -->Comparison Helpers
Basic Comparisons
- eq: Equality comparison
{{#if (eq value1 value2)}}
Values are equal
{{/if}}- gt: Greater than comparison
{{#if (gt value1 value2)}}
Value1 is greater than Value2
{{/if}}- lt: Less than comparison
{{#if (lt value1 value2)}}
Value1 is less than Value2
{{/if}}Conditional Operations
- ternary: Conditional (ternary) operator
{{ternary condition 'Yes' 'No'}}Date and Time
Date Formatting
- formatDate: Formats a date string using dayjs
{{formatDate date 'YYYY-MM-DD'}}
<!-- Output: 2024-11-06 -->
{{formatDate date 'MMM DD, YYYY'}}
<!-- Output: Nov 06, 2024 -->Date Comparisons
- isBefore: Checks if one date is before another
{{#if (isBefore date1 date2)}}
Date1 is before Date2
{{/if}}- isAfter: Checks if one date is after another
{{#if (isAfter date1 date2)}}
Date1 is after Date2
{{/if}}Array Operations
- len: Gets array length
{{len array}} <!-- Output: number of items in array -->Loop Operations
- times: Repeats a block n times
{{#times 3}}
Iteration
{{@index}}
{{/times}}Best Practices
- Always validate input data before using helpers that perform calculations
- Use date formatting helpers with valid date strings
- Handle potential undefined values when using array operations
- Consider performance impact when using complex helpers in loops
- Test helper output with various input types to ensure proper handling