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
Copy file name to clipboardExpand all lines: latte/cs/develop.texy
+64Lines changed: 64 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -216,6 +216,70 @@ Pokud je toto zapnuto, Latte kontroluje vykreslované atributy a vyvolá uživat
216
216
Jakmile jsou všechna varování vyřešena, vypněte varování o migraci a **odstraňte všechny** filtry `|accept` ze svých šablon, protože již nejsou potřeba.
217
217
218
218
219
+
Scopované proměnné cyklu .{data-version:3.1.3}
220
+
==============================================
221
+
222
+
Ve výchozím nastavení zůstávají proměnné definované v cyklu `{foreach}` (jako `$key` a `$value`) dostupné i po jeho skončení – stejně jako v samotném PHP. To může vést k nechtěnému přepsání proměnných, pokud má proměnná cyklu stejný název jako existující proměnná šablony.
223
+
224
+
Funkce `ScopedLoopVariables` omezí platnost proměnných na tělo cyklu. Po jeho skončení se obnoví původní hodnota proměnné (pokud existovala), nebo se proměnná odstraní:
Bez `ScopedLoopVariables`: vypíše `1, 2, 2` (proměnná je přepsána)
240
+
Se `ScopedLoopVariables`: vypíše `1, 2, original` (proměnná je obnovena)
241
+
242
+
Funguje to i s destrukturováním, např. `{foreach $array as [$a, $b]}`.
243
+
244
+
.[note]
245
+
Proměnné cyklu používající reference (`{foreach $array as &$value}`) nebo přiřazení do vlastností (`{foreach $array as $obj->prop}`) nejsou scopovány, protože by to narušilo jejich účel.
Při používání párových značek jako `{if}`, `{foreach}` nebo `{block}` se vnořený obsah často odsazuje pro lepší čitelnost. Toto odsazení se ale ve výchozím nastavení přenáší do vygenerovaného výstupu. Funkce `Dedent` ho automaticky odstraní, takže výstup zůstane čistý bez ohledu na úroveň zanoření v šabloně:
252
+
253
+
```php
254
+
$latte = new Latte\Engine;
255
+
$latte->setFeature(Latte\Feature::Dedent);
256
+
```
257
+
258
+
Příklad:
259
+
260
+
```latte
261
+
{if true}
262
+
Hello
263
+
World
264
+
{/if}
265
+
```
266
+
267
+
Bez `Dedent` by výstup obsahoval odsazení (`\tHello\n\tWorld\n`). S `Dedent` se odsazení odstraní a výstupem je `Hello\nWorld\n`.
268
+
269
+
Hlubší odsazení uvnitř bloku zůstává zachováno relativně k základnímu odsazení:
270
+
271
+
```latte
272
+
{if true}
273
+
Hello
274
+
Indented
275
+
{/if}
276
+
```
277
+
278
+
Výstup: `Hello\n\tIndented\n`.
279
+
280
+
Odsazení v bloku musí být konzistentní (buď tabulátory, nebo mezery). Pokud se mísí, Latte vyhodí výjimku `Inconsistent indentation`.
281
+
282
+
219
283
Překládání v šablonách .{toc: TranslatorExtension}
Filtr funguje jako funkce PHP `array_slice` pro pole nebo `mb_substr` pro řetězce s fallbackem na funkci `iconv_substr` v režimu UTF‑8.
807
+
Filtr funguje jako funkce PHP `array_slice` pro pole nebo `mb_substr` pro řetězce. Pro iterátory vrací generátor – prvky se čtou z původního zdroje jeden po druhém a po dosažení limitu se čtení zastaví. Celý iterátor se do paměti nenačítá.
758
808
759
809
Pokud je start kladný, posloupnost začné posunutá o tento počet od začátku pole/řetezce. Pokud je záporný posloupnost začné posunutá o tolik od konce.
Copy file name to clipboardExpand all lines: latte/cs/syntax.texy
+2Lines changed: 2 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -250,6 +250,8 @@ A co když tag není na řádku sám, ale je tam i další obsah? Bílé znaky p
250
250
251
251
Odsazení je tedy fakticky uvnitř `{if}`: pokud je `$foo` false, nevypíše se nic – ani odsazení, ani prázdný řádek. Pokud je `$foo` true, výstup přirozeně obsahuje odsazení. Prostě pište přehledně odsazené šablony a výstup bude vždy čistý.
252
252
253
+
Pro ještě čistší výstup lze aktivovat funkci [Dedent |develop#Dedent], která odstraní i odsazení vzniklé zanořením v párových značkách jako `{if}` nebo `{foreach}`.
Copy file name to clipboardExpand all lines: latte/en/develop.texy
+64Lines changed: 64 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -216,6 +216,70 @@ When enabled, Latte checks rendered attributes and triggers a user warning (`E_U
216
216
Once all warnings are resolved, disable migration warnings and **remove all** `|accept` filters from your templates, as they are no longer needed.
217
217
218
218
219
+
Scoped Loop Variables .{data-version:3.1.3}
220
+
===========================================
221
+
222
+
By default, variables defined in a `{foreach}` loop (like `$key` and `$value`) remain accessible after the loop ends – just like in PHP itself. This can lead to unintended variable overwrites when a loop variable has the same name as an existing template variable.
223
+
224
+
The `ScopedLoopVariables` feature limits the scope of loop variables to the loop body. After the loop ends, the original variable value is restored (if it existed before), or the variable is unset:
Without `ScopedLoopVariables`: outputs `1, 2, 2` (variable is overwritten)
240
+
With `ScopedLoopVariables`: outputs `1, 2, original` (variable is restored)
241
+
242
+
This also works with destructuring syntax, e.g. `{foreach $array as [$a, $b]}`.
243
+
244
+
.[note]
245
+
Loop variables using references (`{foreach $array as &$value}`) or property assignments (`{foreach $array as $obj->prop}`) are not scoped, as this would break their intended purpose.
When using paired tags like `{if}`, `{foreach}`, or `{block}`, you often indent the nested content for readability. However, this indentation is included in the generated output by default. The `Dedent` feature automatically removes it, so the output stays clean regardless of how deeply you nest your Latte tags:
252
+
253
+
```php
254
+
$latte = new Latte\Engine;
255
+
$latte->setFeature(Latte\Feature::Dedent);
256
+
```
257
+
258
+
Example:
259
+
260
+
```latte
261
+
{if true}
262
+
Hello
263
+
World
264
+
{/if}
265
+
```
266
+
267
+
Without `Dedent`, the output would include the indentation (`\tHello\n\tWorld\n`). With `Dedent`, the indentation is stripped and the output is `Hello\nWorld\n`.
268
+
269
+
Deeper indentation within a block is preserved relative to the base indentation:
270
+
271
+
```latte
272
+
{if true}
273
+
Hello
274
+
Indented
275
+
{/if}
276
+
```
277
+
278
+
Output: `Hello\n\tIndented\n`.
279
+
280
+
Indentation within a block must be consistent (either tabs or spaces). If they are mixed, Latte throws an `Inconsistent indentation` exception.
281
+
282
+
219
283
Translation in Templates .{toc: TranslatorExtension}
Returns the values of a single column `$columnKey` from a multidimensional array as a new array. Can also be used on arrays of objects to extract property values.
267
+
268
+
```latte
269
+
{var $users = [
270
+
[id: 30, name: 'John', age: 30],
271
+
[id: 32, name: 'Jane', age: 25],
272
+
[id: 33, age: 35],
273
+
]}
274
+
275
+
{$users|column: 'name'}
276
+
{* returns ['John', 'Jane'] *}
277
+
278
+
{$users|column: 'name', 'id'}
279
+
{* returns [30 => 'John', 32 => 'Jane'] *}
280
+
```
281
+
282
+
If you pass `null` as the column key, it will reindex the array according to `$indexKey`.
The filter works like the PHP function `array_slice` for arrays or `mb_substr` for strings, with a fallback to the `iconv_substr` function in UTF‑8 mode.
807
+
The filter works like the PHP function `array_slice` for arrays or `mb_substr` for strings. For iterators, it returns a generator – elements are consumed from the source one by one and reading stops once the limit is reached. The entire iterator is never loaded into memory.
758
808
759
809
If `start` is non-negative, the sequence will start at that offset from the beginning of the array/string. If `start` is negative, the sequence will start that far from the end.
Copy file name to clipboardExpand all lines: latte/en/syntax.texy
+2Lines changed: 2 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -250,6 +250,8 @@ What if a tag isn't alone on a line, but appears alongside other content? The wh
250
250
251
251
The indentation is effectively inside `{if}`: when `$foo` is false, nothing is output – not even the indentation or a blank line. When `$foo` is true, the output naturally includes the indentation. You simply write well-structured templates and the output is always clean.
252
252
253
+
For even cleaner output, you can enable the [Dedent |develop#Dedent] feature, which also removes indentation caused by nesting within paired tags like `{if}` or `{foreach}`.
0 commit comments