Skip to content

Commit 9ab8335

Browse files
committed
nette/application 3.2.9 [WIP]
1 parent ec30652 commit 9ab8335

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

application/cs/templates.texy

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,18 @@ Proměnné do šablony předáváme zápisem do `$this->template`. V šabloně j
120120
$this->template->article = $this->articles->getById($id);
121121
```
122122

123+
Pokud chcete, aby se hodnota určité property automaticky předala do šablony jako proměnná, označte ji atributem `#[TemplateVariable]`: .{data-version:3.2.9}
124+
125+
```php
126+
use Nette\Application\Attributes\TemplateVariable;
127+
128+
class ArticlePresenter extends Nette\Application\UI\Presenter
129+
{
130+
#[TemplateVariable]
131+
public string $siteName = 'Můj blog'; // nesmí být private
132+
}
133+
```
134+
123135

124136
Výchozí proměnné
125137
----------------
@@ -244,6 +256,32 @@ protected function beforeRender(): void
244256
}
245257
```
246258

259+
**Pomocí atributů** .{data-version:3.2.9}
260+
261+
Elegantní způsob je definovat filtry a funkce jako metody přímo v presenteru nebo komponentě a označit je atributy:
262+
263+
```php
264+
use Latte\Attributes\TemplateFilter;
265+
use Latte\Attributes\TemplateFunction;
266+
267+
class ArticlePresenter extends Nette\Application\UI\Presenter
268+
{
269+
#[TemplateFilter]
270+
public function money(float $val): string
271+
{
272+
return round($val) . ' Kč';
273+
}
274+
275+
#[TemplateFunction]
276+
public function isWeekend(DateTimeInterface $date): bool
277+
{
278+
return $date->format('N') >= 6
279+
}
280+
}
281+
```
282+
283+
Latte automaticky rozpozná a zaregistruje metody označené těmito atributy. Název filtru nebo funkce v šabloně odpovídá názvu metody. Tyto metody nesmí být privátní.
284+
247285
**Globálně pomocí Extension**
248286

249287
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:

application/en/templates.texy

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,18 @@ Variables are passed to templates by writing them to `$this->template`. They the
120120
$this->template->article = $this->articles->getById($id);
121121
```
122122

123+
To automatically pass a property value to the template as a variable, mark it with the `#[TemplateVariable]` attribute: .{data-version:3.2.9}
124+
125+
```php
126+
use Nette\Application\Attributes\TemplateVariable;
127+
128+
class ArticlePresenter extends Nette\Application\UI\Presenter
129+
{
130+
#[TemplateVariable]
131+
public string $siteName = 'My blog'; // must not be private
132+
}
133+
```
134+
123135

124136
Default Variables
125137
-----------------
@@ -244,6 +256,32 @@ protected function beforeRender(): void
244256
}
245257
```
246258

259+
**Using Attributes** .{data-version:3.2.9}
260+
261+
A more elegant approach is defining filters and functions as methods directly in the presenter or component, marked with attributes:
262+
263+
```php
264+
use Latte\Attributes\TemplateFilter;
265+
use Latte\Attributes\TemplateFunction;
266+
267+
class ArticlePresenter extends Nette\Application\UI\Presenter
268+
{
269+
#[TemplateFilter]
270+
public function money(float $val): string
271+
{
272+
return '$' . number_format($val, 2);
273+
}
274+
275+
#[TemplateFunction]
276+
public function isWeekend(DateTimeInterface $date): bool
277+
{
278+
return $date->format('N') >= 6
279+
}
280+
}
281+
```
282+
283+
Latte automatically discovers and registers methods marked with these attributes. The filter or function name in templates matches the method name. These methods must not be private.
284+
247285
**Globally Using Extensions**
248286

249287
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:

0 commit comments

Comments
 (0)