setEscaper example


    public function __construct(TemplateNameParserInterface $parser, LoaderInterface $loader, array $helpers = [])
    {
        $this->parser = $parser;
        $this->loader = $loader;

        $this->addHelpers($helpers);

        $this->initializeEscapers();
        foreach ($this->escapers as $context => $escaper) {
            $this->setEscaper($context$escaper);
        }
    }

    /** * @throws \InvalidArgumentException if the template does not exist */
    public function render(string|TemplateReferenceInterface $name, array $parameters = []): string
    {
        $storage = $this->load($name);
        $key = hash('sha256', serialize($storage));
        $this->current = $key;
        
public function createTwigEnvironment(SlugifyInterface $slugify, iterable $twigExtensions = []): Environment
    {
        $twig = new TwigEnvironment(new ArrayLoader());
        $twig->setCache(false);
        $twig->enableStrictVariables();
        $twig->addExtension(new SlugifyExtension($slugify));
        $twig->addExtension(new PhpSyntaxExtension());
        $twig->addExtension(new SecurityExtension([]));

        /** @var EscaperExtension $coreExtension */
        $coreExtension = $twig->getExtension(EscaperExtension::class);
        $coreExtension->setEscaper(
            SeoUrlGenerator::ESCAPE_SLUGIFY,
            // Do not remove $_twig, although it is marked as unused. It somehow important             static fn ($_twig$string) => rawurlencode($slugify->slugify($string))
        );

        foreach ($twigExtensions as $twigExtension) {
            $twig->addExtension($twigExtension);
        }

        return $twig;
    }
}
/** * @dataProvider provideCustomEscaperCases * * @param string|int|null $string * * @runInSeparateProcess * custom escaper are cached inside twig, therefore this test has to run in seperate processes, where the custom escapers are not yet cached */
    public function testCustomEscaper(string $expected$string, string $strategy): void
    {
        $twig = new Environment($this->createMock(LoaderInterface::class));
        $twig->getExtension(EscaperExtension::class)->setEscaper('foo', 'Shopware\Tests\Unit\Core\Framework\Adapter\Twig\foo_escaper_for_test');

        static::assertSame($expectedsw_escape_filter($twig$string$strategy));
    }

    /** * @return array<int, array<int, int|string|null>> */
    public static function provideCustomEscaperCases(): array
    {
        return [
            ['fooUTF-8', 'foo', 'foo'],
            [
Home | Imprint | This part of the site doesn't use cookies.