Getting Started with Latte

Latte is a template engine for PHP which eases your work and ensures the output is protected against vulnerabilities, such as XSS.

  • Latte is fast: it compiles templates to plain optimized PHP code.
  • Latte is secure: it is the first PHP engine introducing context-aware escaping and link checking.
  • Latte speaks your language: it has intuitive syntax and helps you to build better websites easily.

Although PHP is originally a templating language, it is not particularly suited for writing templates. Let's have a look at an example of a PHP template that prints an array $items as a list:

<?php if ($items): ?>
	<?php $counter = 1 ?>
	<?php foreach ($items as $item): ?>
		<li id="item-<?php echo $counter++ ?>"><?php
		echo htmlSpecialChars(mb_convert_case($item, MB_CASE_TITLE)) ?>
	<?php endforeach ?>
<?php endif?>

The code is rather confusing. Moreover, we must not forget to call htmlSpecialChars function. That's why there are so many different template engines for PHP. One of the best template engines is part of Nette Framework and it is called Latte. You'll love it!

The same template as the one above can be written easily in Latte:

<ul n:if="$items">
{foreach $items as $item}
	<li id="item-{$iterator->counter}">{$item|capitalize}</li>

As you can see there are two types of directives:

  • tags in braces, for example {foreach …}
  • n:attributes, for example n:if="…"

Installation and Usage

The best way how to install Latte is to download a latest package or use a Composer:

composer require latte/latte

How to render a template? Just run this code:

$latte = new Latte\Engine;


$params = [
	'items' => ['one', 'two', 'three'],

// render to output
$latte->render('template.latte', $params);
// or render to string
$html = $latte->renderToString('template.latte', $params);

Latte automatically regenerates the cache every time you change the template, which can be turned off in the production environment to save a little performance:



Latte is extremely fast. It compiles the templates to native PHP code and stores them in a cache on the disk. So they are as fast as if they would have been written in pure PHP.

The template is automatically recompiled each time we change the source file. While developing you just need to edit the templates in Latte and changes are visible in your browser instantly.

When used on a production server, the initial cache warming may take a while for larger applications. Latte has built-in prevention against cache stampede. This is a situation where server receives a large number of concurrent requests and because Latte's cache does not yet exist, they would all generate it at the same time. Which spikes CPU. Fortunately, Latte works in such a way that for multiple concurrent requests, only the first thread generates cache, the others wait, and then use the cache.


With each error or typo, you will be informed by Tracy with all the luxury. The template source code is displayed and the red line marks the error showing error message as well. With just a single click you can open the template in your favorite editor and fix the error at once. Easy peasy!

If you are using an IDE with code stepping you can go through the generated PHP code of the template.


Latte syntax wasn't invented by engineers but came up from web designer's practical requests. We were looking for the friendliest syntax with which you can write even the most problematic constructions comfortably enough. You will be surprised how much help Latte can be.

You can find tags for advanced layout managing, for template inheritance, nested blocks and so on. Syntax comes from PHP itself so you don't have to learn anything new and you can leverage your know-how.

A pretty Output

Sticklers will enjoy the look of the HTML output which Latte generates. All tags are indented as they are supposed to. The code looks like it has been processed with some kind of HTML code beautifier :-)

Supported PHP versions

version compatible with PHP
Latte 2.8 – 2.9 PHP 7.1 – 8.0
Latte 2.5 – 2.7 PHP 7.1 – 8.0
Latte 2.4 PHP 5.4 – 8.0
Latte 2.3 PHP 5.3 – 7.1

Applies to the latest patch versions.