U
U
ugin_root2020-11-17 08:23:39
PHP
ugin_root, 2020-11-17 08:23:39

How to apply a custom filter after your own?

I have several Twig filters of my own. Some of them return markdown, not text or html.
After applying my filter, I have to call another additional filter every time, something like this:

<span>
    {{ value|my_filter|markdown }}
</span>


If my filter returns, for example, html, then I simply inform through the options that the output of my filter is valid html and does not need to be escaped:
namespace App\Twig;

use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;

class AdvertExtension extends AbstractExtension
{
    public function getFilters():array
    {
        return [
            new TwigFilter('my_filter', [$this, 'my_filter'], ['is_safe' => ['html']]),
        ];
    }

    public function my_filter($value): string
    {
        return sprintf('<b>%s</b>', htmlspecialchars($value));
    }
}


The question is, how can I make it so that after my filter another filter is applied to its output?
Those. so that instead of what I do in the first example, I need to do this:
<span>
    {{ value|my_filter }}
</span>

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question