Functions
cycle
The cycle function is used to loop over an array of values repeatedly.
Usage:
Here's how you can use the cycle function:
{% set start_year = date() | date('Y') %}
{% set end_year = start_year + 5 %}
{% for year in start_year..end_year %}
{{ cycle(['odd', 'even'], loop.index0) }}
{% endfor %}This will output:
odd
even
odd
even
odd
evenThe array passed to cycle can contain any number of values:
{% set fruits = ['apple', 'orange', 'citrus'] %}
{% for i in 0..10 %}
{{ cycle(fruits, i) }}
{% endfor %}Which results in:
apple
orange
citrus
apple
orange
citrus
apple
orange
citrus
apple
orangeArguments:
values: The list of values to cycle on.position: The cycle position.
date
The date function allows conversion of an argument to a date, facilitating date comparisons.
Usage:
To compare dates:
{% if date(user.created_at) < date('-2days') %}
{# do something #}
{% endif %}The provided date argument should match one of the following date and time formats.
Supported Date and Time Formats
ISO 8601 Date:
YYYY-MM-DD(e.g., "2021-05-25")ISO 8601 DateTime:
YYYY-MM-DDTHH:MM:SS(e.g., "2021-05-25T15:30:45")SQL DateTime:
YYYY-MM-DD HH:MM:SS(e.g., "2021-05-25 15:30:45")Common Log Format:
DD/Month/YYYY:HH:MM:SS(e.g., "25/May/2021:15:30:45")Time:
HH:MM(e.g., "15:30"),HH:MM:SS(e.g., "15:30:45")Relative Formats:
now,+1 day,last Monday,first day of next month,3 days ago,+2 weeks,last day of +1 monthDay of the week:
Monday,Tue, etc.Month names:
January,Feb, etc.Relative Keywords:
yesterday,today,tomorrow,Duration:
P1D(1 day),PT2H(2 hours), Combined:P1DT2H(1 day and 2 hours)Miscellaneous:
@1234567890(Unix Timestamp)
You can also specify a timezone as a secondary argument:
{% if date(user.created_at) < date('-2days', 'Europe/Paris') %}
{# do something #}
{% endif %}If no argument is given, the function will return the current date:
{% if date(user.created_at) < date() %}
{# always! #}
{% endif %}Arguments:
date: The date to be converted.timezone: The timezone in which the date should be represented.
max
The max function provides the highest value from a sequence or a set of values.
Usage:
For separate numbers:
{{ max(1, 3, 2) }}For arrays:
{{ max([1, 3, 2]) }}When applied on a hashes, the max function disregards the keys, only comparing the values:
{{ max({2: "e", 1: "a", 3: "b", 5: "d", 4: "c"}) }}
{# returns "e" #}min
The min function provides the smallest value from a sequence or a set of values.
Usage:
For individual numbers:
{{ min(1, 3, 2) }}For arrays:
{{ min([1, 3, 2]) }}When applied on hashes, the min function disregards the keys, only comparing the values:
{{ min({2: "e", 3: "a", 1: "b", 5: "d", 4: "c"}) }}
{# returns "a" #}random
The random function generates a random value, which varies based on the provided argument's type:
A random item from a sequence.
{{ random(['apple', 'orange', 'citrus']) }} {# example output: orange #}A random character from a string.
{{ random('ABC') }} {# example output: C #}A random integer between 0 and the integer parameter (inclusive).
{{ random() }} {# example output: 15386094 #}A random integer between the integer parameter (when negative) and 0 (inclusive).
{{ random(5) }} {# example output: 3 #}A random integer between the first and the second integer parameters (inclusive).
{{ random(50, 100) }} {# example output: 63 #}
Arguments:
values: The values to choose from.max: The maximum value whenvaluesis an integer.
range
The range function produces a list that contains an arithmetic progression of integers.
Basic Usage:
{% for i in range(0, 3) %}
{{ i }},
{% endfor %}This outputs: 0, 1, 2, 3,
Specifying Steps:
When the step value is provided (as the third argument), it dictates the increment (or decrement for negative values):
{% for i in range(0, 6, 2) %}
{{ i }},
{% endfor %}This will produce: 0, 2, 4, 6,
Auto-Decrementing:
If the starting value is larger than the ending value, range automatically assumes a step of -1:
{% for i in range(3, 0) %}
{{ i }},
{% endfor %}Which results in: 3, 2, 1, 0,
Syntax Sugar with the .. Operator:
The template engine provides a handy .. operator which is essentially a shorter form for the range function. It defaults to a step of 1 (or -1 if the starting value is more than the ending value):
{% for i in 0..3 %}
{{ i }},
{% endfor %}Arguments:
low: The starting value of the sequence.high: The maximum potential value of the sequence.step: The difference between consecutive numbers in the sequence.
Last updated