$calc

The $calc function is a powerful expression evaluator that enables complex calculations, string manipulations, array operations, and provides a secure sandbox environment within your templates.

Basic Usage

{{$calc "5 + 3"}}              {{!-- outputs: 8 --}}
{{$ "5 + 3"}}                  {{!-- shorthand syntax --}}

Output Types

The function can return different types of values:

  • Numbers: Mathematical calculations, statistical results

  • Strings: Text manipulation results

  • Arrays: Collection operations

  • Booleans: Conditional evaluations

  • Empty String: Returned for null/undefined values or errors

Variable Interpolation

Use {variableName} syntax to reference context variables:

{{$calc "{price} * 1.2"}}      {{!-- outputs: 120 --}}
{{$calc "{user.age} + 5"}}     {{!-- outputs: 30 --}}

Storing Results

You can store calculation results for later use using the $var function:

Formatting Options

Decimal Places

Custom Number Format (using number formatter patterns)

Error Handling

Function Reference

Mathematical Functions

Basic Arithmetic

  • Addition (+), Subtraction (-), Multiplication (*), Division (/), Modulo (%)

Advanced Math Functions

  • abs(x): Returns absolute value of x

  • max(...args): Returns largest value among arguments

  • min(...args): Returns smallest value among arguments

  • pow(base, exp): Returns base raised to exp power

  • sqrt(x): Returns square root of x

  • round(x): Rounds to nearest integer

  • ceil(x): Rounds up to nearest integer

  • floor(x): Rounds down to nearest integer

String Operations

  • lower(str): Converts string to lowercase

  • upper(str): Converts string to uppercase

  • trim(str): Removes leading/trailing whitespace

  • concat(...args): Joins all arguments into single string

  • slice(str, start, end): Extracts portion of string

  • replace(str, search, replace): Replaces first occurrence of search with replace

Array Operations

  • arr(...args): Creates array from arguments

  • first(arr): Returns first element

  • last(arr): Returns last element

  • len(arr): Returns array length

  • join(arr, separator, finalSeparator): Joins array elements with optional custom separators

  • split(str, separator): Splits string into array

  • sort(arr): Returns sorted array

  • reverse(arr): Returns reversed array

Statistical Functions

  • avg(...nums): Calculates average of numbers

  • med(nums): Calculates median of array

  • sum(nums, [prop]): Sums array elements, optionally extracting property from objects

Type Conversion

  • str(val): Converts to string

  • num(val): Converts to number

  • bool(val): Converts to boolean

Complex Examples

Data Processing Pipeline

Complex Conditional Calculation

Limitations and Safety Features

  • Maximum number size: 1e15

  • Maximum array length: 1000

Best Practices

  • Store intermediate calculations using $var for complex operations

  • Use appropriate formatting options for consistent output

  • Handle potential errors with the error option

  • Break complex calculations into smaller parts

  • Use meaningful variable names

  • Consider performance with large arrays

  • Validate user input before processing

  • Use comments to document complex expressions

Last updated

Was this helpful?