Docs
Deprecated Features
Deprecated Features
This document lists deprecated features in Twig 2.x. Deprecated features are kept for backward compatibility and removed in the next major release (a feature that was deprecated in Twig 2.x is removed in Twig 3.0).
PSR-0
- PSR-0 classes are deprecated in favor of namespaced ones since Twig 2.7.
Inheritance
- Defining a "block" definition in a non-capturing block in a child template is
deprecated since Twig 2.5.0. In Twig 3.0, it will throw a
Twig\Error\SyntaxErrorexception. It does not work anyway, so most projects won't need to do anything to upgrade.
Errors
- Passing a string as the
$sourceargument on\Twig\Error\Error/Twig\Error\Errorconstructor is deprecated since Twig 2.6.1. Pass an instance ofTwig\Sourceinstead.
Tags
- The
spacelesstag is deprecated in Twig 2.7. Use thespacelessfilter instead or{% apply spaceless %}(theTwig\Node\SpacelessNodeandTwig\TokenParser\SpacelessTokenParserclasses are also deprecated). - Using the
spacelesstag at the root level of a child template is deprecated in Twig 2.5.0. This does not work as one would expect it to work anyway. In Twig 3.0, it will throw aTwig\Error\SyntaxErrorexception. - The
filtertag is deprecated in Twig 2.9. Use theapplytag instead (theTwig\TokenParser\FilterTokenParserclasses is also deprecated). - Adding an
ifcondition on afortag is deprecated in Twig 2.10. Use afilterfilter or an "if" condition inside the "for" body instead (if your condition depends on a variable updated inside the loop).
Final Classes
The following classes are marked as @final in Twig 2 and will be final in
3.0:
Twig\Node\ModuleNodeTwig\TwigFilterTwig\TwigFunctionTwig\TwigTestTwig\Profiler\Profile
Parser
- As of Twig 2.7, the
\Twig\Parser::isReservedMacroName()/Twig\Parserfunction is deprecated and will be removed in Twig 3.0. It always returnsfalseanyway as Twig 2 does not have any reserved macro names.
Environment
- As of Twig 2.7, the
base_template_classoption onTwig\Environmentis deprecated and will be removed in Twig 3.0. - As of Twig 2.7, the
Twig\Environment::getBaseTemplateClass()andTwig\Environment::setBaseTemplateClass()methods are deprecated and will be removed in Twig 3.0. - As of Twig 2.7, the
Twig\Environment::getTemplateClass()is marked as being internal and should not be used. - As of Twig 2.7, passing a
Twig\Templateinstance to theTwig\Environment::load()andTwig\Environment::resolveTemplate()is deprecated. - Depending on the input,
Twig\Environment::resolveTemplate()can return aTwig\Templateor aTwig\TemplateWrapperinstance. In Twig 3.0, this method will always return aTwig\TemplateWrapperinstance. You should only rely on the methods of this class if you want to be forward-compatible.
Interfaces
- As of Twig 2.7, the empty
Twig\Loader\ExistsLoaderInterfaceinterface is deprecated and will be removed in Twig 3.0. - As of Twig 2.7, the
Twig\Extension\InitRuntimeInterfaceinterface is deprecated and will be removed in Twig 3.0.
Extensions
- As of Twig 2.11, the
Twig\Extension\CoreExtension::setEscaper()andTwig\Extension\CoreExtension::getEscapers()are deprecated. Use the same methods onTwig\Extension\EscaperExtensioninstead.
Miscellaneous
- As of Twig 2.7, the
Twig_SimpleFilter,Twig_SimpleFunction, andTwig_SimpleTestempty classes are deprecated and will be removed in Twig 3.0. UseTwig\TwigFilter,Twig\TwigFunction, andTwig\TwigTestrespectively. - As of Twig 2.8.2, all usage of
Twig\Loader\FilesystemLoader::findTemplate()check for anullreturn value (same meaning as returningfalse). If you are overiddingTwig\Loader\FilesystemLoader::findTemplate(), you must returnnullinstead offalseto be compatible with Twig 3.0.