Six months after the first alpha, Twig 4.0 reaches beta. Since then, the 3.x branch has deprecated many legacy behaviors, and this beta removes them all: sandboxed templates get an environment of their own, macros become as strict as functions, and several extension points get native types. It also includes every change released in Twig 3.x so far.
Extension, bundle, and application authors can now check their code and templates against what Twig 4.0 should look like. Fix the deprecation notices reported by the latest Twig 3.x version first: code that triggers none should work on 4.0.
The Sandbox and Macros
The biggest changes of this beta were already presented on this blog, as 3.x started deprecating what they replace:
- A First-Class Sandbox: untrusted templates are rendered through a
Sandboxthat owns a dedicated environment, and therender_sandboxed()function replaces thesandboxedargument ofinclude(); - A Stricter Sandbox: tests and the
parent(),block(), andattribute()functions are checked against the policy, and harmless built-ins are always allowed; - A New Macro System: macros handle their arguments like functions, and
mistakes such as calling a macro without parentheses throw a
SyntaxError.
Since these posts, the attributes of the loop variable have also become
always allowed in a sandbox, so policies no longer need to allow the methods of
the loop object.
Faster for Loops and a Better else Clause
The loop object of the new for loop came at a cost: every loop ran
its body in a closure and iterated through a LoopIterator. Loops that don't
use the loop variable now iterate natively, and the ones that only read its
attributes no longer need a closure. Rendering a form with Symfony's
form_div_layout.html.twig runs more than 20% fewer instructions. Loops
also behave like on 3.x again: the sequence is rewound only once, and macros
imported in the body remain available after the loop.
When a loop has a condition, its else clause is now also rendered when the
condition skips every item:
1 2 3 4 5 6 7
<ul>
{% for user in users if user.active %}
<li>{{ user.username }}</li>
{% else %}
<li><em>no active user</em></li>
{% endfor %}
</ul>
Other Changes for Extension and Library Authors
Many smaller changes remove the legacy paths deprecated in 3.x:
- Cloning a
Twig\Environmentthrows an exception; create a new environment instead; TemplateWrapper::unwrap()requires the environment as an argument;- The
autoescapetag rejects a strategy that is neither a string norfalseat compile time; Twig\Markupisfinal;- Compiled nodes must use
yield: Twig no longer checks them forechoorprint, so the#[YieldReady]and#[FirstClassTwigCallableReady]attributes are inert; their classes are kept so code supporting both Twig 3 and 4 still passes static analysis; IntegrationTestCaseandNodeTestCasedeclare native return types, and their data providers are namedprovideTests()andprovideLegacyTests();TwigCallableInterfaceandTokenParserInterfaceget anisAlwaysAllowedInSandbox()method, and the deprecation getters ofTwigCallableInterfaceare replaced byAbstractTwigCallable::triggerDeprecation();IncludeNode::addGetTemplate(),FilesystemLoader::addPath(), andFilesystemLoader::prependPath()get a new argument that overriding methods must declare;MacroNodecan no longer be extended, and theTemplateVariableandAssignTemplateVariablenodes are replaced byMacroVariableandAssignMacroVariable.
The CHANGELOG lists every change, and the deprecation page of Twig 3.x explains how to migrate.
Full Changelog
These pull requests were merged into the 4.x branch since 4.0.0-alpha1; the release also includes all the changes of Twig 3.x up to 3.31.1.
- #5008 Speed up restoring the context after
forloops that set no variables (@nicolas-grekas, @fabpot) - #5007 Speed up
forloops that use theloopvariable without recursing (@nicolas-grekas, @fabpot) - #5006 Speed up reading the attributes of the
loopvariable (@nicolas-grekas) - #5005 Speed up iterating over the sequence of
forloops that use theloopvariable (@nicolas-grekas) - #5004 Speed up
forloops that do not use theloopvariable (@nicolas-grekas, @fabpot) - #4971 Always allow the safe built-in tags, filters, and functions in a sandbox (@fabpot)
- #4970 Name the
IntegrationTestCasedata providersprovideTests()andprovideLegacyTests()like on 3.x (@fabpot) - #4969 Fix the CHANGELOG entry about
Environment::getGlobals()(@fabpot) - #4968 Remove the opcache preload hint for a class
ExtensionSetno longer extends (@fabpot) - #4967 Render the
forelseclause when the loop condition skips every item (@fabpot) - #4966 Always allow the
loopvariable in sandboxed templates (@fabpot) - #4961 Reject an
autoescapestrategy that is neither a string norfalseat compile time (@fabpot) - #4957 4.x cleanup (@fabpot)
- #4941 Stop detecting
echoandprintin compiled nodes (@fabpot) - #4913 Fix include node signature (@fabpot)
- #4912 Cleanup PHPUnit compatibility (@fabpot)
- #4867 Fix PHPStan errors on 4.x (@fabpot)
- #4866 Drop useless PHP comparisons (@homersimpsons)
- #4839 Enforce the sandbox
Markupexception for every security policy (@fabpot)