Skip to content

Add support for object and mapping destructuring#4744

Merged
fabpot merged 1 commit intotwigphp:3.xfrom
fabpot:assignment-operator-object-destructuring
Jan 23, 2026
Merged

Add support for object and mapping destructuring#4744
fabpot merged 1 commit intotwigphp:3.xfrom
fabpot:assignment-operator-object-destructuring

Conversation

@fabpot
Copy link
Copy Markdown
Contributor

@fabpot fabpot commented Jan 21, 2026

Refs #3399

@fabpot fabpot merged commit b609ae9 into twigphp:3.x Jan 23, 2026
41 checks passed
@fabpot fabpot deleted the assignment-operator-object-destructuring branch January 23, 2026 11:57

.. code-block:: twig

{% do {name, email} = user %}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why doing that with {% do %} rather than with {%set %} to make it clear that we are doing assignments of variables ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the cost of the deprecation might too high and thus not worth it ;)

Copy link
Copy Markdown
Member

@stof stof Jan 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants