Search code examples
phpsymfonytwig

Deprecation of twig filters, use Twig_SimpleFilter instead


I'm using Symfony 2.3 and have deprecation warnings in my profiler related to Twig. Such as:

DEPRECATION - Using "replace" with character by character replacement is deprecated 
and will be removed in Twig 2.0

And the |replace tag seems to be still part of the Twig documentation, so I am a little confused on how to deal with this Warning.

Also, I get similar Deprecation Warnings from third-party bundles that I use.

  • Is there an easy way to fix this?
  • Is this going to be an issue if I upgrade from 2.3 to 2.7?

Solution

  • You need to change the way you pass the arguments to replace function in twig :

    • before : {{str | replace ('a','b') }}
    • now : {{str | replace ({'a': 'b'}) }}

    Before it was two arguments, now it's an array.

    You can check in the code source that it's not the replace twig_replace_filter function wich is deprecated but only one way of calling it : https://github.com/twigphp/Twig/blob/1.x/lib/Twig/Extension/Core.php#L534

    Note that the twig doc display the right example : http://twig.sensiolabs.org/doc/filters/replace.html