2015-05-20 2 views
1

Я пытаюсь показать меню моих Связок, но мне нужно показать только те пакеты, которые активны, как я могу получить активные Связки в Twig?Active Bundles of Symfony2

Спасибо и с уважением!

ответ

1

Список пучков хранится в ядре.

Вы должны создать расширение прутого BundleExtension и передать ядро, как зависимость:

<?php 

namespace MyBundle\Twig\Extension; 

use Symfony\Component\HttpKernel\KernelInterface; 

class BundleExtension extends \Twig_Extension 
{ 

    protected $kernel; 

    public function __construct(KernelInterface $kernel) 
    { 
     $this->kernel = $kernel; 
    } 

    /** 
    * {@inheritdoc} 
    * @see Twig_Extension::getFunctions() 
    */ 
    public function getFunctions() 
    { 
     return array(
      'getBundles' => new \Twig_SimpleFunction('getBundles', array($this, 'getBundles')), 
     ); 
    } 

    public function getBundles() 
    { 
     return $this->kernel->getBundles(); 
    } 

    /** 
    * {@inheritdoc} 
    * @see Twig_ExtensionInterface::getName() 
    */ 
    public function getName() 
    { 
     return 'get_bundles'; 
    } 
} 

зарегистрировать его в качестве услуги:

services: 
    bundle_extension: 
     class: MyBundle\Twig\Extension\BundleExtension 
     arguments: ['@kernel'] 
     tags: 
      - { name: twig.extension }  

И теперь в шаблоне веточки:

{% set bundles = getBundles() %} 
{% for bundle in bundles %} 
    {{ bundle.getName()}}<br/> 
{% endfor %} 
+0

Спасибо, Мед, он отлично работает. – LuzEterna

+1

FYI: '{{bundle.name}}' будет делать то же самое, что '{{bundle.getName()}}'. – qooplmao

Смежные вопросы