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:
Which results in:
Arguments:
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:
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 no argument is given, the function will return the current date:
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:
For arrays:
When applied on a hashes, the max function disregards the keys, only comparing the values:
min
The min function provides the smallest value from a sequence or a set of values.
Usage:
For individual numbers:
For arrays:
When applied on hashes, the min function disregards the keys, only comparing the values:
random
The random function generates a random value, which varies based on the provided argument's type:
A random item from a sequence.
A random character from a string.
A random integer between 0 and the integer parameter (inclusive).
A random integer between the integer parameter (when negative) and 0 (inclusive).
A random integer between the first and the second integer parameters (inclusive).
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:
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):
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:
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):
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