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 or array transformation
truncate (length, append = '…') shortens the length preserving whole words
substr (offset [, length]) returns part of the string
trim (charset = whitespace) strips whitespace or other characters from the beginning and end of the string
stripHtml removes HTML tags and converts HTML entities to text
strip removes whitespace, similar to spaceless tag
indent (level = 1, char = "\t") indents the text from left with number of tabs
replace (search, replace = '') replaces all occurrences of the search string with the replacement
replaceRE (pattern, replace = '') replaces all occurrences according to regular expression
padLeft (length, pad = ' ') completes the string to given length from left
padRight (length, pad = ' ') completes the string to given length from right
repeat (count) repeats the string
implode (glue = '') joins an array to a string
webalize adjusts the UTF-8 string to the shape used in the URL
breakLines Inserts HTML line breaks before all newlines
reverse reverse an UTF-8 string or array
length returns length of a string or array
batch (array, length [, item]) listing linear data in a table
Letter casing
lower makes a string lower case
upper makes a string upper case
firstUpper makes the first letter upper case
capitalize lower case, the first letter of each word upper case
date (format) formats date
number (decimals = 0, decPoint = '.') format number
bytes (precision = 2) formats size in bytes
dataStream (mimetype = detect) Data URI protocol conversion
noescape prints a variable without escaping
escapeUrl escapes parameter in URL

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.

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

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 called modifier (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:



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?


Removes unnecessary whitespace from the output.

{block |strip}


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


Indents a text from left by a given number of tabs or other characters which we specify in the second optional argument:

{block |indent}




The filter removes the diacritics and replaces all the characters except the letters of the English alphabet and digits by a hyphen. Requires the installation of nette/utils.

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


Inserts HTML line breaks before all newlines. Input text is automatically escaped.

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


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

Letter casing

{var $s = 'blue COLOR'}
{$s|lower} blue color
{$s|upper} BLUE COLOR
{$s|firstUpper} Blue COLOR
{$s|capitalize} Blue Color


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.



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


Formats a size in bytes to human-readable form.

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


Converts the data to URI scheme. With this filter you can insert images to HTML or CSS without linking external files. All the modern browsers. Requires PHP extension fileinfo.

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


Simplifies the listing of linear data into a table. The first parameter is the number of columns, the second (optional) additional value to fill the last row.

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


        <td>No item</td>
        <td>No item</td>


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

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


Prevents automatic URL sanitization. Latte automatically checks if the variable contains a web URL (ie. HTTP 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