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

Fabien Potencier
Contributed by Fabien Potencier in #4854 , #4803 , #4813 , #4819 , #4816 , #4839 , #4966 , #4971 , #4851 , #4873 , #4877 and #4878

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 Sandbox that owns a dedicated environment, and the render_sandboxed() function replaces the sandboxed argument of include();
  • A Stricter Sandbox: tests and the parent(), block(), and attribute() 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

Nicolas Grekas Fabien Potencier
Contributed by Nicolas Grekas and Fabien Potencier in #4967 , #5004 , #5005 , #5006 , #5007 and #5008

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

Fabien Potencier Nicolas Grekas
Contributed by Fabien Potencier and Nicolas Grekas in #4840 , #4910 , #4912 , #4913 , #4929 , #4941 , #4951 , #4961 and #4970

Many smaller changes remove the legacy paths deprecated in 3.x:

  • Cloning a Twig\Environment throws an exception; create a new environment instead;
  • TemplateWrapper::unwrap() requires the environment as an argument;
  • The autoescape tag rejects a strategy that is neither a string nor false at compile time;
  • Twig\Markup is final;
  • Compiled nodes must use yield: Twig no longer checks them for echo or print, so the #[YieldReady] and #[FirstClassTwigCallableReady] attributes are inert; their classes are kept so code supporting both Twig 3 and 4 still passes static analysis;
  • IntegrationTestCase and NodeTestCase declare native return types, and their data providers are named provideTests() and provideLegacyTests();
  • TwigCallableInterface and TokenParserInterface get an isAlwaysAllowedInSandbox() method, and the deprecation getters of TwigCallableInterface are replaced by AbstractTwigCallable::triggerDeprecation();
  • IncludeNode::addGetTemplate(), FilesystemLoader::addPath(), and FilesystemLoader::prependPath() get a new argument that overriding methods must declare;
  • MacroNode can no longer be extended, and the TemplateVariable and AssignTemplateVariable nodes are replaced by MacroVariable and AssignMacroVariable.

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 for loops that set no variables (@nicolas-grekas, @fabpot)
  • #5007 Speed up for loops that use the loop variable without recursing (@nicolas-grekas, @fabpot)
  • #5006 Speed up reading the attributes of the loop variable (@nicolas-grekas)
  • #5005 Speed up iterating over the sequence of for loops that use the loop variable (@nicolas-grekas)
  • #5004 Speed up for loops that do not use the loop variable (@nicolas-grekas, @fabpot)
  • #4971 Always allow the safe built-in tags, filters, and functions in a sandbox (@fabpot)
  • #4970 Name the IntegrationTestCase data providers provideTests() and provideLegacyTests() like on 3.x (@fabpot)
  • #4969 Fix the CHANGELOG entry about Environment::getGlobals() (@fabpot)
  • #4968 Remove the opcache preload hint for a class ExtensionSet no longer extends (@fabpot)
  • #4967 Render the for else clause when the loop condition skips every item (@fabpot)
  • #4966 Always allow the loop variable in sandboxed templates (@fabpot)
  • #4961 Reject an autoescape strategy that is neither a string nor false at compile time (@fabpot)
  • #4957 4.x cleanup (@fabpot)
  • #4941 Stop detecting echo and print in 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 Markup exception for every security policy (@fabpot)
Published in #Releases #Twig