gpt4 book ai didi

symfony - 如何检查 Twig 中是否存在捆绑?

转载 作者:行者123 更新时间:2023-12-04 22:05:05 24 4
gpt4 key购买 nike

我的应用程序由八个包组成,在我的主布局中,我想检查某个包是否存在,以便我可以包含一个子模板,我该怎么做?

最佳答案

感谢@DonCallisto,我决定在我的模板中使用一个twig函数,以下是我的twig扩展。

<?php

namespace MG\AdminBundle\Twig;

use Symfony\Component\DependencyInjection\ContainerInterface;
class Bundles extends \Twig_Extension {
protected $container;

public function __construct(ContainerInterface $container) {
$this->container = $container;
}

public function getFunctions()
{
return array(
new \Twig_SimpleFunction(
'bundleExists',
array($this, 'bundleExists')
),
);
}


public function bundleExists($bundle){
return array_key_exists(
$bundle,
$this->container->getParameter('kernel.bundles')
);
}
public function getName() {
return 'mg_admin_bundles';
}
}

然后我在我的 services.yml 中注册了它
services:                
mg_admin.bundles.extension:
class: MG\AdminBundle\Twig\Bundles
arguments: [@service_container]
tags:
- { name: twig.extension }

现在在我的 Twig 模板中,我可以像这样检查已注册的包:
{% if bundleExists('MGEmailBundle') %}
{% include 'MGEmailBundle:SideBar:sidebar.html.twig' %}
{% endif %}

关于symfony - 如何检查 Twig 中是否存在捆绑?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21615374/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com