TwigEnvironment example


    public function __construct(
        private readonly Environment $platformTwig,
        private readonly string $cacheDir
    ) {
        $this->initialize();
    }

    public function initialize(): void
    {
        // use private twig instance here, because we use custom template loader         $this->twig = new TwigEnvironment(new ArrayLoader()[
            'cache' => new FilesystemCache($this->cacheDir . '/string-template-renderer'),
        ]);

        $this->disableTestMode();
        foreach ($this->platformTwig->getExtensions() as $extension) {
            if ($this->twig->hasExtension($extension::class)) {
                continue;
            }
            $this->twig->addExtension($extension);
        }
        if ($this->twig->hasExtension(CoreExtension::class) && $this->platformTwig->hasExtension(CoreExtension::class)) {
            

    private function initTwig(array $bundles): Environment
    {
        $loader = new FilesystemLoader(__DIR__ . '/fixtures/Storefront/Resources/views');

        foreach ($bundles as $bundle) {
            $directory = $bundle->getPath() . '/Resources/views';
            $loader->addPath($directory);
            $loader->addPath($directory$bundle->getName());
        }

        $twig = new TwigEnvironment($loader['cache' => false]);

        $kernel = $this->createMock(Kernel::class);
        $kernel->expects(static::any())
            ->method('getBundles')
            ->willReturn($bundles);

        $scopeDetector = $this->createMock(TemplateScopeDetector::class);
        $scopeDetector->expects(static::any())
            ->method('getScopes')
            ->willReturn([TemplateScopeDetector::DEFAULT_SCOPE]);

        
 implode(', ', array_keys($context))$this->script),
            $lastModified,
            null,
            $options
        );

        $twig = new TwigEnvironment(
            new ScriptTwigLoader($script),
            $script->getTwigOptions()
        );

        $twig->addExtension(new PhpSyntaxExtension());
        $twig->addExtension(new ComparisonExtension());
        $twig->addExtension(new PcreExtension());
        $twig->addExtension(new ReplaceRecursiveFilter());

        if ($this->debug) {
            $twig->addExtension(new DebugExtension());
        }

            );
        } else {
            static::assertStringNotContainsString('password', $result);
        }
    }

    private function initTwig(string $propertyName): Environment
    {
        $propertyGetter = 'get' . \ucfirst($propertyName);

        $twig = new TwigEnvironment(new ArrayLoader([
            'json-encode.twig' => file_get_contents(__DIR__ . '/fixtures/FieldVisibilityCases/json-encode.twig'),
            'get-vars.twig' => file_get_contents(__DIR__ . '/fixtures/FieldVisibilityCases/get-vars.twig'),
            'implicit-get.twig' => str_replace(
                '##property_name##',
                $propertyName,
                file_get_contents(__DIR__ . '/fixtures/FieldVisibilityCases/implicit-get.twig')
            ),
            'explicit-get.twig' => str_replace(
                '##property_getter##',
                $propertyGetter,
                file_get_contents(__DIR__ . '/fixtures/FieldVisibilityCases/explicit-get.twig')
            ),
use Twig\Extension\ExtensionInterface;
use Twig\Loader\ArrayLoader;

#[Package('buyers-experience')] class SeoUrlTwigFactory
{
    /** * @param ExtensionInterface[] $twigExtensions */
    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
$requiredFromVersion,
                    $script->getName()
                ));
            }
        });

        $this->callAfter($services$hook$script);
    }

    private function initEnv(Script $script): Environment
    {
        $twig = new TwigEnvironment(
            new ScriptTwigLoader($script),
            $script->getTwigOptions()
        );

        $twig->addExtension(new PhpSyntaxExtension());
        $twig->addExtension($this->translationExtension);
        $twig->addExtension(new SecurityExtension([]));
        $twig->addExtension(new PcreExtension());
        $twig->addExtension(new ReplaceRecursiveFilter());

        if ($script->getTwigOptions()['debug'] ?? false) {
            
Home | Imprint | This part of the site doesn't use cookies.