KnpLabs / KnpRadBundle

Rapid Application Development for Symfony2 [UNMAINTAINED]

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Auto registration Twig extensions with arguments

opened this issue · comments

How to do this?

You can implement ContainerAwareInterface, from container get services, parameters. For example Router service.

<?php
namespace App\Twig;

use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Routing\Router;

/**
 * Twig main extension
 */
class AmountFieldExtension extends  \Twig_Extension implements ContainerAwareInterface
{
    /** @var ContainerInterface $container */
    private $container;

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

    /**
     * @return Router
     */
    protected function getRouter()
    {
        return $this->container->get('router');
    }

yes, @vkalmuk example is the only possibility.

As soon as you ned arguments, you need to create the service yourself.