Override bundle template from another bundle in symfony 3.4/4

ぃ、小莉子 提交于 2019-12-21 17:38:50

问题


because bundle inheritance is deprecated since Symfony 3.4 and will be removed in 4.0, I'm finding a new solution. I have:

  • Application
  • FooBundle
  • BarBundle

And I haven't problem with overriding templates in Application. But I need to override templates from BarBundle in FooBundle. It was so easy with bundle inheritance but I'm lost now :)

I tried twig namespaces - no success, but I configured it in wrong way maybe. My goal is to have base templates in BarBundle that I can override in FooBundle or Application or both. (it is because BarBundle is third-party bundle and FooBundle is my bundle used in many projects).

Is it still possible without bundle inheritance and how?

Thanks.


回答1:


So I have recently needed same functionality and with help from comment @NicoHasse I managed to make working example

In your bundle bundle extension class you need to implement PrependExtensionInterface and then you can modify twig paths. Then you need to know original namespace your you need to override (php bin/console debug:twig).

You can confirm its working with twig debug command where you should see your path at first place of that namespace.

class YourExtensionClass extends Extension implements PrependExtensionInterface
{
    public function prepend(ContainerBuilder $container)
    {
        $container->loadFromExtension('twig', [
            'paths' => [
                '%kernel.project_dir%/vendor/xx/yy/zzz' => 'OriginalVNamespace',
            ]
        ]);



回答2:


Here's a more comprehensible example

Let's start with making some assumptions

Your bundle's name: AcmeBundle

Bundle you want to override: FOSUserBundle

Run command php bin/console debug:twig and find the namespace of the bundle you want to override. In this case it's @FOSUser.

Your bundle extension should look like this

<?php // src/AcmeBundle/DependencyInjection/AcmeExtension.php

namespace AcmeBundle\DependencyInjection;

// ...

use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;

class AcmeExtension extends Extension implements PrependExtensionInterface
{
    // ...

    public function prepend(ContainerBuilder $container)
    {
        $container->loadFromExtension('twig', array(
            'paths' => array(
                '%kernel.project_dir%/src/AcmeBundle/Resources/FOSUserBundle/views' => 'FOSUser', // You use the namespace you found earlier here. Discard the `@` symbol.
            ),
        ));
    }
}

Now you can create src/AcmeBundle/Resources/FOSUserBundle/views/Security/login.html.twig to override the login template of FOSUserBundle.

This was just an example for FOSUserBundle. You can change bundle names depending on what you're trying to override.



来源:https://stackoverflow.com/questions/50605939/override-bundle-template-from-another-bundle-in-symfony-3-4-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!