You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Takto jednoduše můžeme do šablon předat jakékoliv proměnné. Při vývoji robustních aplikací ale bývá užitečnější se omezit. Například tak, že explicitně nadefinujeme výčet proměnných, které šablona očekává, a jejich typů. Díky tomu nám bude moci PHP kontrolovat typy, IDE správně našeptávat a statická analýza odhalovat chyby.
123
+
124
+
Výchozí proměnné
125
+
----------------
126
+
127
+
Presentery a komponenty předávají do šablon několik užitečných proměnných automaticky:
128
+
129
+
- `$basePath` je absolutní URL cesta ke kořenovému adresáři (např. `/eshop`)
130
+
- `$baseUrl` je absolutní URL ke kořenovému adresáři (např. `http://localhost/eshop`)
131
+
- `$user` je objekt [reprezentující uživatele |security:authentication]
132
+
- `$presenter` je aktuální presenter
133
+
- `$control` je aktuální komponenta nebo presenter
134
+
- `$flashes` pole [zpráv |presenters#Flash zprávy] zaslaných funkcí `flashMessage()`
135
+
136
+
Pokud používáte vlastní třídu šablony, tyto proměnné se předají, pokud pro ně vytvoříte property.
137
+
138
+
139
+
Typově bezpečné proměnné
140
+
------------------------
141
+
142
+
Při vývoji robustních aplikací je užitečné explicitně nadefinovat, jaké proměnné šablona očekává a jakého jsou typu. Získáte tak typovou kontrolu v PHP, chytré našeptávání v IDE a schopnost statické analýzy odhalovat chyby.
124
143
125
144
A jak takový výčet nadefinujeme? Jednoduše v podobě třídy a její properties. Pojmenujeme ji podobně jako presenter, jen s `Template` na konci:
126
145
@@ -169,21 +188,6 @@ public function renderDefault(): void
169
188
```
170
189
171
190
172
-
Výchozí proměnné
173
-
----------------
174
-
175
-
Presentery a komponenty předávají do šablon několik užitečných proměnných automaticky:
176
-
177
-
- `$basePath` je absolutní URL cesta ke kořenovému adresáři (např. `/eshop`)
178
-
- `$baseUrl` je absolutní URL ke kořenovému adresáři (např. `http://localhost/eshop`)
179
-
- `$user` je objekt [reprezentující uživatele |security:authentication]
180
-
- `$presenter` je aktuální presenter
181
-
- `$control` je aktuální komponenta nebo presenter
182
-
- `$flashes` pole [zpráv |presenters#Flash zprávy] zaslaných funkcí `flashMessage()`
183
-
184
-
Pokud používáte vlastní třídu šablony, tyto proměnné se předají, pokud pro ně vytvoříte property.
185
-
186
-
187
191
Vytváření odkazů
188
192
----------------
189
193
@@ -205,21 +209,44 @@ Více informací najdete v kapitole [Vytváření odkazů URL|creating-links].
205
209
Vlastní filtry, značky apod.
206
210
----------------------------
207
211
208
-
Šablonovací systém Latte lze rozšířit o vlastní filtry, funkce, značky apod. Lze tak učinit přímo v metodě `render<View>` nebo `beforeRender()`:
212
+
Šablonovací systém Latte lze rozšířit o vlastní filtry, funkce, značky a další prvky. K dispozici jsou tři způsoby, jak to udělat, od nejrychlejších ad-hoc řešení až po architektonický přístup pro celou aplikaci.
213
+
214
+
**Ad-hoc v metodách presenteru**
215
+
216
+
Nejrychlejší způsob je přidat filtr nebo funkci přímo v kódu presenteru či komponenty. V presenteru je k tomu vhodná metoda `beforeRender()` nebo `render<View>()`:
Pro složitější logiku můžete konfigurovat přímo objekt `Latte\Engine`:
215
238
216
-
// nebo konfigurujeme přímo objekt Latte\Engine
239
+
```php
240
+
protected function beforeRender(): void
241
+
{
217
242
$latte = $this->template->getLatte();
218
243
$latte->setMigrationWarnings();
219
244
}
220
245
```
221
246
222
-
Latte ve verzi 3 nabízí pokročilejší způsob a to vytvoření si [extension |latte:extending-latte#Latte Extension] pro každý webový projekt. Kusý příklad takové třídy:
247
+
**Globálně pomocí Extension**
248
+
249
+
Předchozí způsoby jsou vhodné pro filtry a funkce, které potřebujete jen v konkrétním presenteru nebo komponentě, nikoliv v celé aplikaci. Pro celou aplikaci je nejvhodnější vytvořit si [extension |latte:extending-latte#Latte Extension]. Jde o třídu, která centralizuje všechna rozšíření Latte pro celý projekt. Kusý příklad:
223
250
224
251
```php
225
252
namespace App\Presentation\Accessory;
@@ -251,18 +278,25 @@ final class LatteExtension extends Latte\Extension
251
278
];
252
279
}
253
280
281
+
private function filterTimeAgoInWords(DateTimeInterface $time): string
282
+
{
283
+
// ...
284
+
}
285
+
254
286
// ...
255
287
}
256
288
```
257
289
258
-
Zaregistrujeme ji pomocí [konfigurace |configuration#Šablony Latte]:
290
+
Extension zaregistrujeme pomocí [konfigurace |configuration#Šablony Latte]:
259
291
260
292
```neon
261
293
latte:
262
294
extensions:
263
295
- App\Presentation\Accessory\LatteExtension
264
296
```
265
297
298
+
Výhodou extension je, že lze využít dependency injection, mít přístup k modelové vrstvě aplikace a všechna rozšíření mít přehledně na jednom místě. Extension umožnuje definovat i vlastní značky, providery, průchody pro Latte kompilátor a další.
Copy file name to clipboardExpand all lines: application/en/templates.texy
+59-25Lines changed: 59 additions & 25 deletions
Original file line number
Diff line number
Diff line change
@@ -111,16 +111,35 @@ Using `$this->setLayout(false)` or the `{layout none}` tag inside the template d
111
111
The files where layout templates are looked up can be changed by overriding the [formatLayoutTemplateFiles() |api:Nette\Application\UI\Presenter::formatLayoutTemplateFiles()] method, which returns an array of possible file names.
112
112
113
113
114
-
Variables in the Template
115
-
-------------------------
114
+
Template Variables
115
+
------------------
116
116
117
-
Variables are passed to the template by writing them to `$this->template`, and then they are available in the template as local variables:
117
+
Variables are passed to templates by writing them to `$this->template`. They then become available in the template as local variables:
This way, we can easily pass any variables to templates. However, when developing robust applications, it is often more useful to impose limitations. For example, by explicitly defining a list of variables that the template expects and their types. This allows PHP to perform type checking, the IDE to provide correct autocompletion, and static analysis to detect errors.
123
+
124
+
Default Variables
125
+
-----------------
126
+
127
+
Presenters and components automatically pass several useful variables to templates:
128
+
129
+
- `$basePath` is the absolute URL path to the root directory (e.g., `/eshop`)
130
+
- `$baseUrl` is the absolute URL to the root directory (e.g., `http://localhost/eshop`)
131
+
- `$user` is an object [representing the user |security:authentication]
132
+
- `$presenter` is the current presenter
133
+
- `$control` is the current component or presenter
134
+
- `$flashes` is an array of [messages |presenters#Flash Messages] sent by the `flashMessage()` function
135
+
136
+
If you use a custom template class, these variables are passed if you create a property for them.
137
+
138
+
139
+
Type-Safe Variables
140
+
-------------------
141
+
142
+
When developing robust applications, it's useful to explicitly define which variables the template expects and their types. This provides type checking in PHP, smart hints in your IDE, and enables static analysis to catch errors.
124
143
125
144
And how do we define such a list? Simply in the form of a class and its properties. We name it similarly to the presenter, but with `Template` at the end:
126
145
@@ -169,21 +188,6 @@ public function renderDefault(): void
169
188
```
170
189
171
190
172
-
Default Variables
173
-
-----------------
174
-
175
-
Presenters and components automatically pass several useful variables to templates:
176
-
177
-
- `$basePath` is the absolute URL path to the root directory (e.g., `/eshop`)
178
-
- `$baseUrl` is the absolute URL to the root directory (e.g., `http://localhost/eshop`)
179
-
- `$user` is an object [representing the user |security:authentication]
180
-
- `$presenter` is the current presenter
181
-
- `$control` is the current component or presenter
182
-
- `$flashes` is an array of [messages |presenters#Flash Messages] sent by the `flashMessage()` function
183
-
184
-
If you use a custom template class, these variables are passed if you create a property for them.
185
-
186
-
187
191
Creating Links
188
192
--------------
189
193
@@ -205,21 +209,44 @@ More information can be found in the chapter [Creating URL Links|creating-links]
205
209
Custom Filters, Tags, etc.
206
210
--------------------------
207
211
208
-
The Latte templating system can be extended with custom filters, functions, tags, etc. This can be done directly in the `render<View>` or `beforeRender()` method:
212
+
The Latte templating system can be extended with custom filters, functions, tags, and other elements. There are three approaches available, ranging from quick ad-hoc solutions to architectural patterns for entire applications.
213
+
214
+
**Ad-hoc in Presenter Methods**
215
+
216
+
The quickest approach is adding filters or functions directly in presenter or component code. In presenters, the `beforeRender()` or `render<View>()` methods work well for this:
For more complex logic, you can configure the `Latte\Engine` object directly:
215
238
216
-
// or configure the Latte\Engine object directly
239
+
```php
240
+
protected function beforeRender(): void
241
+
{
217
242
$latte = $this->template->getLatte();
218
243
$latte->setMigrationWarnings();
219
244
}
220
245
```
221
246
222
-
Latte version 3 offers a more advanced way by creating an [extension |latte:extending-latte#Latte Extension] for each web project. Here is a brief example of such a class:
247
+
**Globally Using Extensions**
248
+
249
+
The previous approaches suit filters and functions needed only in specific presenters or components, not application-wide. For the entire application, creating an [extension |latte:extending-latte#Latte Extension] works best. This class centralizes all Latte extensions for your project. A brief example:
223
250
224
251
```php
225
252
namespace App\Presentation\Accessory;
@@ -251,18 +278,25 @@ final class LatteExtension extends Latte\Extension
251
278
];
252
279
}
253
280
281
+
private function filterTimeAgoInWords(DateTimeInterface $time): string
282
+
{
283
+
// ...
284
+
}
285
+
254
286
// ...
255
287
}
256
288
```
257
289
258
-
We register it using [configuration |configuration#Latte Templates]:
290
+
Register the extension through [configuration |configuration#Latte Templates]:
259
291
260
292
```neon
261
293
latte:
262
294
extensions:
263
295
- App\Presentation\Accessory\LatteExtension
264
296
```
265
297
298
+
Extensions offer several advantages: dependency injection support, access to your application's model layer, and centralized management of all extensions. They also support custom tags, providers, compiler passes, and more.
0 commit comments