Latte Filters

Filters are functions that change or format the data to a form we want. This is summary of the built-in filters which are available.

String / array transformation
batch listing linear data in a table
breakLines Inserts HTML line breaks before all newlines
bytes formats size in bytes
clamp clamps value to the range
dataStream Data URI protocol conversion
date formats date
implode joins an array to a string
indent indents the text from left with number of tabs
length returns length of a string or array
number formats number
padLeft completes the string to given length from left
padRight completes the string to given length from right
repeat repeats the string
replace replaces all occurrences of the search string with the replacement
replaceRE replaces all occurrences according to regular expression
reverse reverses an UTF‑8 string or array
sort sorts an array
strip removes whitespace, similar to spaceless tag
stripHtml removes HTML tags and converts HTML entities to text
substr returns part of the string
trim strips whitespace from the string
truncate shortens the length preserving whole words
webalize adjusts the UTF‑8 string to the shape used in the URL
Letter casing
capitalize lower case, the first letter of each word upper case
firstUpper makes the first letter upper case
lower makes a string lower case
upper makes a string upper case
escapeUrl escapes parameter in URL
noescape prints a variable without escaping

There are also escaping filters for HTML (escapeHtml and escapeHtmlComment), XML (escapeXml), JavaScript (escapeJs), CSS (escapeCss) and iCalendar (escapeICal), which Latte uses itself thanks to context-aware escaping and you do not need to write them.

checkUrl sanitizes string for use inside href attribute
noCheck prevents automatic URL sanitization

Latte the src and href attributes checks automatically, so you almost don't need to use the checkUrl filter.

All built-in filters work with UTF‑8 encoded strings.


Latte allows calling filters by using the pipe sign notation (preceding space is allowed):


Filters can be chained, in that case they apply in order from left to right:


Parameters are put after the filter name separated by colon or comma:


Filters can be applied on expression:

{var $name = ($title|upper) . ($subtitle|lower)}</h1>

Custom filters can be registered this way:

$latte = new Latte\Engine;
$latte->addFilter('shortify', function (string $s, int $len = 10): string {
	return mb_substr($s, 0, $len);

We use it in a template like this:



batch (int length, mixed item)array

Filter that simplifies the listing of linear data in the form of a table. It returns an array of array with the given number of items. If you provide a second parameter this is used to fill up missing items on the last row.

{var $items = ['a', 'b', 'c', 'd', 'e']}
{foreach ($items|batch: 3, 'No item') as $row}
		{foreach $row as $column}


		<td>No item</td>


Inserts HTML line breaks before all newlines.

{var $s = "Text & with \n newline"}
{$s|breakLines}    {* outputs "Text &amp; with <br>\n newline" *}

bytes (int precision = 2)

Formats a size in bytes to human-readable form.

{$size|bytes}     0 B, 1.25 GB, …
{$size|bytes:0}   10 B, 1 GB, …

clamp (int|float min, int|float max)

Returns value clamped to the inclusive range of min and max.

{$level|clamp: 0, 255}

Also exists as function:

{=clamp($level, 0, 255)}

dataStream (string mimetype = detect)

Converts the content to data URI scheme. It can be used to insert images into HTML or CSS without the need to link external files.

Lets have an image in a variable $img = Image::fromFile('obrazek.gif'), then

<img src="{$img|dataStream}">

generates for example:

<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA

Requires PHP extension fileinfo.

date (string format)

Returns a date in the given format using options of strftime or date PHP functions. Filter gets a date as a UNIX timestamp, a string or an object of DateTime type.

{$today|date:'j. n. Y'}

implode (string glue = '')

Return a string which is the concatenation of the strings in the array. The separator between elements is an empty string per default, you can define it with the optional parameter.

{=[1, 2, 3]|implode}      {* outputs '123' *}
{=[1, 2, 3]|implode:'|'}  {* outputs '1|2|3' *}

indent (int level = 1, string char = "\t")

Indents a text from left by a given number of tabs or other characters which we specify in the second optional argument. Blank lines are not indented.

{block |indent}




Returns length of a string or array.

  • for strings, it will return length in UTF‑8 characters
  • for arrays, it will return count of items
  • for objects that implement the Countable interface, it will use the return value of the count()
  • for objects that implement the IteratorAggregate interface, it will use the return value of the iterator_count()
{if ($users|length) > 10}

number (int decimals = 0, string decPoint = '.', string thousandsSep = ',')

Formats a number to given number of decimal places. You can also specify a character of the decimal point and thousands separator.

{1234.20 |number}              1,234
{1234.20 |number:1}            1,234.2
{1234.20 |number:2}            1,234.20
{1234.20 |number:2, ',', ' '}  1 234,20

padLeft (int length, string pad = ' ')

Pads a string to a certain length with another string from left.

{='hello'|padLeft: 10, '123'}  {* outputs '12312hello' *}

padRight (int length, string pad = ' ')

Pads a string to a certain length with another string from right.

{='hello'|padRight: 10, '123'}  {* outputs 'hello12312' *}

repeat (int count)

Repeats the string x-times.

{='hello'|repeat: 3}  {* outputs 'hellohellohello' *}

replace (string search, string replace = '')

Replaces all occurrences of the search string with the replacement string.

{='hello world'|replace: 'world', 'friend'}  {* outputs 'hello friend' *}

replaceRE (string pattern, string replace = '')

Replaces all occurrences according to regular expression.

{='hello world'|replaceRE: 'l.*', 'l'}  {* outputs 'hel' *}


Reverses given string or array.

{var $s = 'Nette'}
{$s|reverse}    {* outputs 'etteN' *}
{var $a = ['N', 'e', 't', 't', 'e']}
{$a|reverse}    {* returns ['e', 't', 't', 'e', 'N'] *}


Filter that sorts array.

{foreach ($names|sort) as $name}

Array sorted in reverse order.

{foreach ($names|sort|reverse) as $name}


Removes unnecessary whitespace from the output.

{block |strip}


<ul> <li>Hello</li> </ul>


Removes HTML tags and converts HTML entities to text.

{='<p>one &lt; two</p>'|stripHtml}  {* outputs 'one < two' *}

substr (int offset, int length = null)

Extracts a slice of a string. Works as the PHP function mb_substr with fallback to iconv_substr in UTF‑8 mode.

{$string|substr: 1, 2}


Strip leading and trailing characters, by default whitespace.

{='  I like Latte.  '|trim}    {* outputs 'I like Latte.' *}
{='  I like Latte.'|trim: '.'} {* outputs '  I like Latte' *}

truncate (int length, string append = '…')

Shortens a string to the maximum given length but tries to preserve whole words. If the string is truncated it adds ellipsis at the end (this can be changed by the second parameter).

{var $title = 'Hello, how are you?'}
{$title|truncate:5}  {* Hell…                *}
{$title|truncate:17} {* Hello, how are…      *}
{$title|truncate:30} {* Hello, how are you?  *}


Converts to ASCII. Converts spaces to hyphens. Removes characters that aren’t alphanumerics, underscores, or hyphens. Converts to lowercase. Also strips leading and trailing whitespace.

{var $s = 'Our 10. product'}
{$s|webalize}    {* outputs 'our-10-product' *}

Requires package nette/utils.

Letter Casing

Requires PHP extension mbstring.


Returns a title-cased version of the value. Words will start with uppercase letters, all remaining characters are lowercase.

{='i like LATTE'|capitalize}  {* outputs 'I Like Latte' *}


Converts a first letter of value to uppercase.

{='the latte'|upper}  {* outputs 'The latte' *}


Converts a value to lowercase.

{='LATTE'|lower}   {* outputs 'latte' *}


Converts a value to uppercase.

{='latte'|upper}  {* outputs 'LATTE' *}



Escapes a variable to be used as a parameter in URL.

<a href="{$name|escapeUrl}">{$name}</a>


Disables automatic escaping.



Enforces URL sanitization. It checks if the variable contains a web URL (ie. HTTP/HTTPS protocol) and prevents the writing of links that may pose a security risk.

{var $link = 'javascript:window.close()'}
<a data-href="{$link|checkUrl}">checked</a>
<a data-href="{$link}">unchecked</a>


<a data-href="">checked</a>
<a data-href="javascript:window.close()">unchecked</a>


Prevents automatic URL sanitization. Latte automatically checks if the variable contains a web URL (ie. HTTP/HTTPS protocol) and prevents the writing of links that may pose a security risk.

If the link uses a different scheme, such as javascript: or data:, and you are sure of its contents, you can disable the check via |noCheck.

{var $link = 'javascript:window.close()'}

<a href="{$link}">checked</a>
<a href="{$link|noCheck}">unchecked</a>


<a href="">checked</a>
<a href="javascript:window.close()">unchecked</a>
Improve this page