Latte Functions
In addition to common PHP functions, you can also use these functions in templates.
Usage
Functions are used in the same way as common PHP functions and can be used in all expressions:
<p>{clamp($num, 1, 100)}</p>
{if odd($num)} ... {/if}
Custom functions can be registered this way:
$latte = new Latte\Engine;
$latte->addFunction('shortify', fn(string $s, int $len = 10) => mb_substr($s, 0, $len));
It is then called in the template like this:
<p>{shortify($text)}</p>
<p>{shortify($text, 100)}</p>
Functions
clamp (int|float $value, int|float $min, int|float $max): int|float
Clamps a value to the given inclusive range of min and max.
{=clamp($level, 0, 255)}
See also the clamp filter.
divisibleBy (int $value, int $by): bool
Checks if a variable is divisible by a number.
{if divisibleBy($num, 5)} ... {/if}
even (int $value): bool
Checks if the given number is even.
{if even($num)} ... {/if}
first (string|iterable $value): mixed
Returns the first element of an array or the first character of a string:
{=first([1, 2, 3, 4])} {* outputs 1 *}
{=first('abcd')} {* outputs 'a' *}
See also last, first filter.
group (iterable $data, string|int|\Closure $by): array
This function groups data according to different criteria.
In this example, rows in the table are grouped by the categoryId
column. The output is an array of arrays, where
the key is the value in the categoryId
column. Read the
detailed guide.
{foreach group($items, categoryId) as $categoryId => $categoryItems}
<ul>
{foreach $categoryItems as $item}
<li>{$item->name}</li>
{/foreach}
</ul>
{/foreach}
See also the group filter.
hasBlock (string $name): bool
Checks if a block of the specified name exists:
{if hasBlock(header)} ... {/if}
See also block existence check.
last (string|array $value): mixed
Returns the last element of an array or the last character of a string:
{=last([1, 2, 3, 4])} {* outputs 4 *}
{=last('abcd')} {* outputs 'd' *}
See also first, last filter.
odd (int $value): bool
Checks if the given number is odd.
{if odd($num)} ... {/if}
slice (string|array $value, int $start, ?int $length=null, bool $preserveKeys=false): string|array
Extracts a slice of an array or a string.
{=slice('hello', 1, 2)} {* outputs 'el' *}
{=slice(['a', 'b', 'c'], 1, 2)} {* outputs ['b', 'c'] *}
The function works like the PHP function array_slice
for arrays or mb_substr
for strings, with a
fallback to the iconv_substr
function in UTF‑8 mode.
If start
is non-negative, the sequence will start at that offset from the beginning of the array/string. If
start
is negative, the sequence will start that far from the end.
If length
is given and is positive, then the sequence will have up to that many elements. If the input is shorter
than the length
, then only the available elements will be present. If length
is given and is negative,
the sequence will stop that many elements from the end of the input. If it is omitted, the sequence will have everything from
start
up until the end of the input.
By default, the function reorders and resets the integer array keys. This behavior can be changed by setting
preserveKeys
to true. String keys are always preserved, regardless of this parameter.