Context helpers that extend the core Dust.js templating system with conditional logic, mathematical operations, and iteration utilities.
Mathematical operations helper that performs arithmetic calculations within templates with full precision control and error handling.
Performs mathematical operations on numeric values with optional rounding and conditional rendering support.
/**
* Performs mathematical operations on numeric values
* @param key - First operand value (required)
* @param method - Mathematical operation to perform (required)
* @param operand - Second operand value (required for binary operations)
* @param round - If truthy, rounds the result to nearest integer
*/
{@math key="operand1" method="operation" operand="operand2" round="boolean"/}
// With conditional body (acts like @select helper)
{@math key="operand1" method="operation" operand="operand2"}
{@eq value="expected"}Result matches expected value{/eq}
{@gt value="threshold"}Result is above threshold{/gt}
{/math}Supported Operations:
add: Addition (key + operand)subtract: Subtraction (key - operand)multiply: Multiplication (key * operand)divide: Division (key / operand) - logs error if operand is 0mod: Modulo (key % operand) - logs error if operand is 0ceil: Ceiling (Math.ceil(key)) - operand not requiredfloor: Floor (Math.floor(key)) - operand not requiredround: Round (Math.round(key)) - operand not requiredabs: Absolute value (Math.abs(key)) - operand not requiredtoint: Convert to integer (parseInt(key, 10)) - operand not requiredUsage Examples:
// Basic arithmetic operations
{@math key="15" method="add" operand="5"/}
<!-- Output: 20 -->
{@math key="100" method="divide" operand="3" round="true"/}
<!-- Output: 33 -->
// Using context variables
{@math key="{price}" method="multiply" operand="{quantity}"/}
// Single operand operations
{@math key="-42" method="abs"/}
<!-- Output: 42 -->
{@math key="3.7" method="floor"/}
<!-- Output: 3 -->
// With conditional body
{@math key="{score}" method="mod" operand="10"}
{@eq value="0"}Score is divisible by 10{/eq}
{@ne value="0"}Score has remainder: {.}{/ne}
{/math}
// Complex calculation with variables
{@math key="{$idx}" method="mod" operand="2"}
{@eq value="0"}Even row{/eq}
{@eq value="1"}Odd row{/eq}
{/math}Error Handling:
NaN and logs error message "{@math}: Division by 0""{@math}: \key` or `method` was not provided"` and returns empty output"{@math}: \key` or `method` was not provided"` and returns empty output"{@math}: Method \methodName` is not supported"` and returns empty outputparseFloat() - may result in NaN outputundefined, then convert to NaNType Coercion:
All key and operand values are processed as follows:
context.resolve()parseFloat()NaNNaN propagate NaN to the resultLogging:
All error messages are logged using dust.log(helperName, message, level) where:
helperName is "math"message describes the specific errorlevel is "ERROR" for critical issues like missing parametersInstall with Tessl CLI
npx tessl i tessl/npm-dustjs-helpers