Skip to content

Commit a95425b

Browse files
committed
latte 3.1.3
1 parent 485a244 commit a95425b

6 files changed

Lines changed: 240 additions & 8 deletions

File tree

latte/cs/develop.texy

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,70 @@ Pokud je toto zapnuto, Latte kontroluje vykreslované atributy a vyvolá uživat
216216
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.
217217

218218

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í:
225+
226+
```php
227+
$latte = new Latte\Engine;
228+
$latte->setFeature(Latte\Feature::ScopedLoopVariables);
229+
```
230+
231+
Příklad rozdílu:
232+
233+
```latte
234+
{var $item = 'original'}
235+
{foreach [1, 2] as $item}{$item}, {/foreach}
236+
{$item}
237+
```
238+
239+
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.
246+
247+
248+
Automatické odsazení (Dedent) .{toc: Dedent}{data-version:3.1.3}
249+
================================================================
250+
251+
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+
219283
Překládání v šablonách .{toc: TranslatorExtension}
220284
==================================================
221285

latte/cs/filters.texy

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ V šablonách můžeme používat funkce, které pomáhají upravit nebo přefor
1010
| `breakLines` | [Před konce řádku přidá HTML odřádkování |#breakLines]
1111
| `bytes` | [formátuje velikost v bajtech |#bytes]
1212
| `clamp` | [ohraničí hodnotu do daného rozsahu |#clamp]
13+
| `column` | [extrahuje jeden sloupec z pole |#column]
14+
| `commas` | [spojí pole čárkami |#commas]
1315
| `dataStream` | [konverze pro Data URI protokol |#dataStream]
1416
| `date` | [formátuje datum a čas |#date]
1517
| `explode` | [rozdělí řetězec na pole podle oddělovače |#explode]
@@ -259,6 +261,50 @@ Ohraničí hodnotu do daného inkluzivního rozsahu min a max.
259261
Existuje také jako [funkce |functions#clamp].
260262

261263

264+
column(string|int|null $columnKey, string|int|null $indexKey=null) .[filter]{data-version:3.1.3}
265+
------------------------------------------------------------------------------------------------
266+
Vrátí z vícerozměrného pole hodnoty jednoho sloupce `$columnKey` jako nové pole. Lze použít i na pole objektů pro získání hodnot vlastností.
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+
{* vrátí ['John', 'Jane'] *}
277+
278+
{$users|column: 'name', 'id'}
279+
{* vrátí [30 => 'John', 32 => 'Jane'] *}
280+
```
281+
282+
Pokud předáte `null` jako klíč sloupce, přeindexuje pole podle `$indexKey`.
283+
284+
285+
commas(?string $lastGlue=null) .[filter]{data-version:3.1.3}
286+
------------------------------------------------------------
287+
Spojí prvky pole čárkou a mezerou (`', '`). Jde o pohodlnou zkratku pro běžný výpis položek v čitelné podobě.
288+
289+
```latte
290+
{var $items = ['jablka', 'pomeranče', 'banány']}
291+
{$items|commas}
292+
{* vypíše 'jablka, pomeranče, banány' *}
293+
```
294+
295+
Lze zadat i vlastní oddělovač pro poslední dvojici položek:
296+
297+
```latte
298+
{$items|commas: ' a '}
299+
{* vypíše 'jablka, pomeranče a banány' *}
300+
301+
{=['PHP', 'JavaScript', 'Python']|commas: ', nebo '}
302+
{* vypíše 'PHP, JavaScript, nebo Python' *}
303+
```
304+
305+
Viz také [#implode].
306+
307+
262308
dataStream(string $mimetype=detect) .[filter]
263309
---------------------------------------------
264310
Konvertuje obsah do data URI scheme. Pomocí něj lze do HTML nebo CSS vkládat obrázky bez nutnosti linkovat externí soubory.
@@ -407,6 +453,8 @@ Můžete také použít alias `join`:
407453
{=[1, 2, 3]|join} {* vypíše '123' *}
408454
```
409455

456+
Viz také [#commas], [#explode].
457+
410458

411459
indent(int $level=1, string $char="\t") .[filter]
412460
-------------------------------------------------
@@ -637,19 +685,21 @@ Pamatujte, že skutečný vzhled čísel se může lišit podle nastavení země
637685

638686
padLeft(int $length, string $pad=' ') .[filter]
639687
-----------------------------------------------
640-
Doplní řetězec do určité délky jiným řetězcem zleva.
688+
Doplní řetězec nebo číslo do určité délky jiným řetězcem zleva.
641689

642690
```latte
643691
{='hello'|padLeft: 10, '123'} {* vypíše '12312hello' *}
692+
{=123|padLeft: 5, '0'} {* vypíše '00123' *}
644693
```
645694

646695

647696
padRight(int $length, string $pad=' ') .[filter]
648697
------------------------------------------------
649-
Doplní řetězec do určité délky jiným řetězcem zprava.
698+
Doplní řetězec nebo číslo do určité délky jiným řetězcem zprava.
650699

651700
```latte
652701
{='hello'|padRight: 10, '123'} {* vypíše 'hello12312' *}
702+
{=123|padRight: 5, '0'} {* vypíše '12300' *}
653703
```
654704

655705

@@ -747,14 +797,14 @@ Viz také [#ceil], [#floor].
747797

748798
slice(int $start, ?int $length=null, bool $preserveKeys=false) .[filter]
749799
------------------------------------------------------------------------
750-
Extrahuje část pole nebo řetězce.
800+
Extrahuje část pole, řetězce nebo iterátoru.
751801

752802
```latte
753803
{='hello'|slice: 1, 2} {* vypíše 'el' *}
754804
{=['a', 'b', 'c']|slice: 1, 2} {* vypíše ['b', 'c'] *}
755805
```
756806

757-
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á.
758808

759809
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.
760810

latte/cs/syntax.texy

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff 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
250250

251251
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ý.
252252

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}`.
254+
253255

254256
Syntaktický cukr
255257
================

latte/en/develop.texy

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,70 @@ When enabled, Latte checks rendered attributes and triggers a user warning (`E_U
216216
Once all warnings are resolved, disable migration warnings and **remove all** `|accept` filters from your templates, as they are no longer needed.
217217

218218

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:
225+
226+
```php
227+
$latte = new Latte\Engine;
228+
$latte->setFeature(Latte\Feature::ScopedLoopVariables);
229+
```
230+
231+
Example of the difference:
232+
233+
```latte
234+
{var $item = 'original'}
235+
{foreach [1, 2] as $item}{$item}, {/foreach}
236+
{$item}
237+
```
238+
239+
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.
246+
247+
248+
Automatic Dedentation .{toc: Dedent}{data-version:3.1.3}
249+
========================================================
250+
251+
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+
219283
Translation in Templates .{toc: TranslatorExtension}
220284
====================================================
221285

latte/en/filters.texy

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ In templates, we can use functions that help modify or reformat data into its fi
1010
| `breakLines` | [Inserts HTML line breaks before all newlines |#breakLines]
1111
| `bytes` | [formats size in bytes |#bytes]
1212
| `clamp` | [clamps a value to the given range |#clamp]
13+
| `column` | [extracts a single column from an array |#column]
14+
| `commas` | [joins an array with commas |#commas]
1315
| `dataStream` | [Data URI protocol conversion |#dataStream]
1416
| `date` | [formats the date and time |#date]
1517
| `explode` | [splits a string into an array by a delimiter |#explode]
@@ -259,6 +261,50 @@ Clamps a value to the given inclusive range of min and max.
259261
Also exists as a [function |functions#clamp].
260262

261263

264+
column(string|int|null $columnKey, string|int|null $indexKey=null) .[filter]{data-version:3.1.3}
265+
------------------------------------------------------------------------------------------------
266+
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`.
283+
284+
285+
commas(?string $lastGlue=null) .[filter]{data-version:3.1.3}
286+
------------------------------------------------------------
287+
Joins array elements with a comma and space (`', '`). A convenient shortcut for listing items in a human-readable format.
288+
289+
```latte
290+
{var $items = ['apples', 'oranges', 'bananas']}
291+
{$items|commas}
292+
{* outputs 'apples, oranges, bananas' *}
293+
```
294+
295+
You can also provide a custom separator for the last pair of items:
296+
297+
```latte
298+
{$items|commas: ' and '}
299+
{* outputs 'apples, oranges and bananas' *}
300+
301+
{=['PHP', 'JavaScript', 'Python']|commas: ', or '}
302+
{* outputs 'PHP, JavaScript, or Python' *}
303+
```
304+
305+
See also [#implode].
306+
307+
262308
dataStream(string $mimetype='detect') .[filter]
263309
-----------------------------------------------
264310
Converts content to the data URI scheme. This allows embedding images into HTML or CSS without needing to link external files.
@@ -407,6 +453,8 @@ You can also use the alias `join`:
407453
{=[1, 2, 3]|join} {* outputs '123' *}
408454
```
409455

456+
See also [#commas], [#explode].
457+
410458

411459
indent(int $level=1, string $char="\t") .[filter]
412460
-------------------------------------------------
@@ -637,19 +685,21 @@ Remember that the actual appearance of numbers may vary depending on the country
637685

638686
padLeft(int $length, string $pad=' ') .[filter]
639687
-----------------------------------------------
640-
Pads a string to a certain length with another string from the left.
688+
Pads a string or number to a certain length with another string from the left.
641689

642690
```latte
643691
{='hello'|padLeft: 10, '123'} {* outputs '12312hello' *}
692+
{=123|padLeft: 5, '0'} {* outputs '00123' *}
644693
```
645694

646695

647696
padRight(int $length, string $pad=' ') .[filter]
648697
------------------------------------------------
649-
Pads a string to a certain length with another string from the right.
698+
Pads a string or number to a certain length with another string from the right.
650699

651700
```latte
652701
{='hello'|padRight: 10, '123'} {* outputs 'hello12312' *}
702+
{=123|padRight: 5, '0'} {* outputs '12300' *}
653703
```
654704

655705

@@ -747,14 +797,14 @@ See also [#ceil], [#floor].
747797

748798
slice(int $start, ?int $length=null, bool $preserveKeys=false) .[filter]
749799
------------------------------------------------------------------------
750-
Extracts a slice of an array or a string.
800+
Extracts a slice of an array, string, or iterator.
751801

752802
```latte
753803
{='hello'|slice: 1, 2} {* outputs 'el' *}
754804
{=['a', 'b', 'c']|slice: 1, 2} {* outputs ['b', 'c'] *}
755805
```
756806

757-
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.
758808

759809
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.
760810

latte/en/syntax.texy

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,8 @@ What if a tag isn't alone on a line, but appears alongside other content? The wh
250250

251251
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.
252252

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}`.
254+
253255

254256
Syntactic Sugar
255257
===============

0 commit comments

Comments
 (0)