Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# 3.31.0 (2026-XX-XX)

* Speed up the `with` tag by adding the globals to the new context in place
* Fix `html_cva()` ignoring a boolean default variant
* Fix a PHP 8.5 deprecation in `html_cva()` when a recipe value is `null`
* Add the `collate` function to `twig/intl-extra` to compare strings in the alphabetical order of a locale
Expand Down
6 changes: 5 additions & 1 deletion src/Node/WithNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,11 @@ public function compile(Compiler $compiler): void
$compiler->write("\$context = [];\n");
}

$compiler->write(\sprintf("\$context = \$%s + \$context + \$this->env->getGlobals();\n", $varsName));
// two statements: adding the globals in place copies the context once instead of twice
$compiler
->write(\sprintf("\$context = \$%s + \$context;\n", $varsName))
->write("\$context += \$this->env->getGlobals();\n")
Comment thread
nicolas-grekas marked this conversation as resolved.
Resolved
;
}

$compiler
Expand Down
15 changes: 15 additions & 0 deletions tests/Fixtures/tags/with/precedence.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
--TEST--
"with" tag gives precedence to its variables, then to the context, then to the globals
--TEMPLATE--
[{% with {global: 'with'} %}{{ global }}{% endwith %}]
{% set global = 'context' %}
[{% with {foo: 'with'} %}{{ global }}{% endwith %}]
[{% with {foo: 'with'} only %}{{ global }}{% endwith %}]
[{{ global }}]
--DATA--
return []
--EXPECT--
[with]
[context]
[global]
[context]
Loading