Add support for object and mapping destructuring#4744
Conversation
|
|
||
| .. code-block:: twig | ||
|
|
||
| {% do {name, email} = user %} |
There was a problem hiding this comment.
why doing that with {% do %} rather than with {%set %} to make it clear that we are doing assignments of variables ?
There was a problem hiding this comment.
My reasonning is that it's just an expression like any other one (it can be "embed" in a another expression).
I was thinking about maybe deprecating the set tag and add a capture tag for when you want to capture a chunk of a template.
There was a problem hiding this comment.
the cost of the deprecation might too high and thus not worth it ;)
There was a problem hiding this comment.
Oh, I missed the fact that assignment expressions are now a thing in the dev version of Twig.
I actually liked the fact that arbitrary expressions don't change the context (it also makes it simpler to think about scopes, especially in combination with expressions used in custom tags, as the affected scope there might depend on the internal implementation of the tag regarding its usage of the expression).
Having {% set %} to assign variables gives a much more explicit intent when reading the code than using {% do %} for everything (not counting the fact that it is too early to deprecate {% set %} given the huge impact on the ecosystem and the fact that assignment expressions are fairly new and so would require dropping support for many Twig versions in the ecosystem to perform the migration)
Refs #3399