Type System

Type system is main thing for the development of robust applications. Latte brings type support to templates. To knowing what data or object type each variable is allows

Two points that significantly improve the quality and convenience of development.

How to start using types? Create a template class, eg CatalogTemplate, representing the passed parameters:

class CatalogTemplate
{
	/** @var array */
	public $langs;

	/** @var ProductEntity[] */
	public $products;
}

$params = new CatalogTemplate;
$params->langs = $settings->getLanguages();
$params->products = $entityManager->getRepository('Product')->findAll();
$latte->render('template.latte', $params);

Then insert the {templateType} tag with the full class name (including the namespace) at the beginning of the template:

{templateType MyApp\CatalogTemplate}

We have defined that there will be variables $langs and $products in the template including the corresponding types. Now the IDE can correctly autocomplete.

You can specify the types of local variables using tags {var}, {varType}, {define}:

{var string $name = $article->getTitle()}
{varType Nette\Security\User $user}

How to save work? How to write a template class or {varType} tags as easily as possible? Get them generated. That is precisely what pair of tags {templatePrint} and {varPrint} do. If you place one of these tags in a template, the code of class or template is displayed instead of the normal rendering. Then simply select and copy the code into your project.

(since Latte 2.7)

Improve this page