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.

The declared types are informative and Latte does not check them at this time.

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

class CatalogTemplateParameters
{
	public function __construct(
		public string $langs,
		/** @var ProductEntity[] */
		public array $products,
		public Address $address,
	) {}
}

$latte->render('template.latte', new CatalogTemplateParameters(
	address: $userAddress,
	lang: $settings->getLanguage(),
	products: $entityManager->getRepository('Product')->findAll(),
));

Then insert the {templateType} tag with the full class name (including the namespace) at the beginning of the template. This defines that there are be variables $langs and $products in the template including the corresponding types. You can also specify the types of local variables using tags {var}, {varType} and {define}.

Now the IDE can correctly autocomplete.

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.

{templateType}

The types of parameters passed to the template are declared using class:

{templateType MyApp\CatalogTemplateParameters}

{varType}

How to declare types of variables? For this purpose use tag {varType} for an existing variable, or {var}:

{varType Nette\Security\User $user}
{varType string $lang}

{templatePrint}

You can also generate this class using the {templatePrint} tag. If you place it at the beginning of the template, the code of class is displayed instead of the normal template. Then simply select and copy the code into your project.

{varPrint}

The {varPrint} tag saves you time. If you place it in a template, the list of {varType} tags is displayed instead of the normal rendering. Then simply select and copy the code into your template.

The {varPrint} lists local variables that are not template parameters. If you want to list all variables, use {varPrint all}.

version: 3.0