Math Handlebar

The Math Handlebar feature empowers content creators to incorporate basic arithmetic operations directly within their templates. This functionality enhances dynamic personalization by allowing calculations based on user-specific attributes (traits), fixed values, or a combination of both. It supports operations such as addition (+), subtraction (-), multiplication (*), and division (/), and can even handle nested expressions and decimal scaling.

This is particularly useful for tailoring messaging in real-time—for example, calculating age from a birth year, estimating loyalty rewards, or adjusting values based on user behavior.

It is essential to ensure that the traits used in Math Handlebar expressions are in numeric format (INTEGER). If a trait contains a non-numeric value (e.g., a string), the math operation will not be rendered correctly, and the handlebar expression will fail.

Syntax and Usage

The general syntax for a math expression is as follows:

{{math <value1> <operator> <value2>}}

Examples

Here are various use cases to demonstrate the flexibility of Math Handlebar:

  • Basic operations with user traits:

{{math user.[traits.example] '+' '20'}}
{{math user.[traits.example] '-' '4'}}
{{math user.[traits.example] '*' '-12'}}
{{math user.[traits.example] '/' '5'}}

  • Floating point with scale option:

{{math user.[traits.example] '/' '3' scale=2}} → Rounds the result to 2 decimal places

  • Nested expressions:

{{math '2' '' (math user.[traits.example] '+' '2')}}
{{math '2' '+' (math '2' '*' '2')}} → Results = 6

  • Combining multiple traits:

{{math user.[traits.example] '+' user.[traits.example2]}}

Practical Applications

  • Reward calculation based on number of actions:

    {{math user.[traits.purchases] '*' '5'}}

  • Personalized discounts based on age:

    {{math 2025 '-' user.[traits.birthYear]}}

  • Time-based messaging, such as days since last login:

    {{math currentDate '-' user.[traits.lastLoginDate]}}


Additionally, please be aware that complex or deeply nested handlebars can impact rendering performance. For optimal results, consider breaking down intricate expressions into smaller, more manageable segments to improve both readability and system efficiency.

Example of complex Math handlebar:

a + b*c/d - {{math(math user.[traits.math] '+' (math (math user.[traits.math] '*' user.[traits.math]) '/' 100)) '/' user.[traits.math]}}