2 years ago

#62868

test-img

EL Med

Symfony 5 override bundle template

I created a new bundle in rootFolder/app/Electroforums/TestBundle/src and I created a Controller and a template to render it inside my controller.

Here is my Controller :

<?php


namespace Electroforums\TestBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Twig\Environment;


class RandomController
{
    protected $twig;

    public function __construct(Environment $twig)
    {
        $this->twig = $twig;
    }

    public function testBundleMethod() {
        $content = $this->twig->render(
            '@EFTest/Test/hello.html.twig'
        );

        return new Response($content);
    }
}

I put then my template in src/Resources/views ..

<!DOCTYPE html>
<html>

<body>
<h3>Yea 2</h3>
{% block content %}
    <h1>Hello Bundle</h1>
{% endblock %}
</body>
</html>

My EFTestExtension class where I prepend the views bundle's path to the configuration.

class EFTestExtension extends Extension implements PrependExtensionInterface
{

    public function load(array $configs, ContainerBuilder $container)
    {
        $fl = new FileLocator(__DIR__."/../Resources/config");
        $loader = new XmlFileLoader($container, $fl);
        $loader->load('services.xml');
    }

    public function prepend(ContainerBuilder $container)
    {
        $twigConfig = [];
        $twigConfig['paths'][__DIR__.'/../Resources/views'] = "EFTest";
        //$twigConfig['paths'][__DIR__.'/../Resources/public'] = "EFTest.public";
        $container->prependExtensionConfig('twig', $twigConfig);
    }

    public function getAlias()
    {
        return parent::getAlias();
    }
}

My template is rendred successfuly but what I want now is to override this template's bundle inside /templates root directory to give the possibility to other developpers to override my bundles's templates.

I tried to put the template inside rootFolder/templates/EFTest/Test/hello.html.twig

How can I do that please ?

php

symfony

templates

overriding

symfony5

0 Answers

Your Answer

Accepted video resources