Skip to main content

Utility Functions

These functions are available directly in any expression.

FunctionDescriptionExample
minSmallest value{{ min(1, 2, 3) }} or {{ min(prices) }}
maxLargest value{{ max(1, 2, 3) }} or {{ max(prices) }}
randomRandom value{{ random() }} or {{ random(items) }}
rangeGenerate a sequence{{ range(1, 10) }} or {{ range(0, 100, 5) }}
cycleCycle through values{{ cycle(['odd', 'even'], loop.index) }}
dateCreate a timestamp{{ date() }} or {{ date('now') }}
html_classesConditional CSS classes{{ html_classes('btn', {active: true}) }}

min / max

Accept either multiple arguments or an array:

{{ min(10, 5, 20) }}       → 5
{{ max(10, 5, 20) }} → 20
{{ min(prices) }} → smallest in array
{{ max(prices) }} → largest in array

random

Returns a random value. Behavior depends on the input type:

{{ random() }}                              → random integer
{{ random(['red', 'green', 'blue']) }} → random element
{{ random(10) }} → random 0..10
{{ random('abcdef') }} → random character

range

Generates a sequence of values. Supports integers, floats, and characters:

{{ range(1, 5) }}           → [1, 2, 3, 4, 5]
{{ range(0, 100, 10) }} → [0, 10, 20, ..., 100]
{{ range('a', 'z') }} → ['a', 'b', 'c', ..., 'z']

Capped at 1,000 items to prevent resource exhaustion.

cycle

Cycles through an array based on an index — useful in loops for alternating values:

{{ cycle(['odd', 'even'], loop.index) }}
{{ cycle(['primary', 'secondary', 'accent'], loop.index) }}

date

Creates a timestamp value that can be formatted with the date filter:

{{ date()|date('Y-m-d') }}
{{ date('now')|date('F j, Y') }}
{{ "now"|date('Y') }}

html_classes

Generates a space-separated class string. Strings are always included, objects include the key only when the value is truthy:

{{ html_classes('btn', {active: is_active, disabled: is_disabled}) }}

If is_active is true and is_disabled is false, the output is btn active.