从Symfony 3.4 / 4中的另一个包覆盖包模板 [英] Override bundle template from another bundle in symfony 3.4/4

查看:55
本文介绍了从Symfony 3.4 / 4中的另一个包覆盖包模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于从Symfony 3.4起不赞成使用包继承,并且将在4.0中删除,因此我在寻找一种新的解决方案。我有:

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


  • 应用程序

  • FooBundle

  • BarBundle

我对覆盖Application中的模板没有问题。但是我需要在FooBundle中覆盖BarBundle中的模板。使用bundle继承是如此简单,但是我现在迷路了:)

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 :)

我尝试了twig名称空间-没有成功,但是我可能以错误的方式配置了它。我的目标是在BarBundle中具有基本模板,可以在FooBundle或Application或两者中覆盖这些模板。 (这是因为BarBundle是第三方捆绑软件,而FooBundle是我在许多项目中使用的捆绑软件)。

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?

谢谢。

推荐答案

所以我最近需要相同的功能并需要注释的帮助@NicoHasse我设法建立了工作示例

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

在您的捆绑包扩展类中,您需要实现 PrependExtensionInterface ,然后您可以修改树枝路径。然后,您需要知道您需要覆盖的原始名称空间( php bin / console debug:twig )。

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).

您可以使用twig debug命令来确认其功能,在该位置您应该在该名称空间的第一位置看到路径。

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',
            ]
        ]);

这篇关于从Symfony 3.4 / 4中的另一个包覆盖包模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆