nette / latte

☕ Latte: the safest & truly intuitive templates for PHP. Engine for those who want the most secure PHP sites.
https://latte.nette.org
Other
1.09k stars 107 forks source link

Add support for array destructuring in tag {var} #337

Closed mildabre closed 9 months ago

mildabre commented 1 year ago

Hi,

the intended purpose of latte tag {var} is to fill values into variables, while on the other hand the purpose of latte tag {do} is just to execute one line of code.

At the time being unfortunately it is not possible to use destructuring array into variables like this:

{var
    [$var1, $var2] = match($key){
        'key1' => ['value11', 'value21'],
        'key2' => ['value12', 'value22'],
        'key3' => ['value13', 'value23'],
    }
}

This destructuring would be in Latte templates useful, because there are many cases when you need put a type into template and insight the template get more than one variables for example a) text message, b) css class.

Now it is possible to use for this purpose tag {do}:

{do
    [$var1, $var2] = match($key){
        'key1' => ['value11', 'value21'],
        'key2' => ['value12', 'value22'],
        'key3' => ['value13', 'value23'],
    }
}

But it would be better to use for assigning variables tag {var}. Destructuring array into variables is natural way of assigning variables in PHP and shoud be supported by the tag {var}.

dg commented 9 months ago

This is very complicated to deal with.