0
votes

I'm trying to create a new function for Twig and am creating a Twig Extension in Symfony2. The service is registered and is injecting the container properly (so I can reference the response object), but it's not finding the function.

Here's the extension:

namespace MyProject\Bundle\MyBundle\Twig\Extensions;

use Twig_Extension,
    Twig_Function_Method;

use Symfony\Component\DependencyInjection\ContainerAwareInterface,
    Symfony\Component\DependencyInjection\ContainerInterface;


class TwigUiExtensions extends Twig_Extension implements ContainerAwareInterface
{
    protected $container;

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

    public function getContainer()
    {
        return $this->container;
    }

    public function getFunctions()
    {
        return array(
            'defaultBodyClasses'    =>  new Twig_Function_Method($this, 'defaultBodyClassesFunction')
        );
    }

    private function _getControllerName()
    {
        $pattern = "#Controller\\\([a-zA-Z]*)Controller#";
        $matches = array();
        preg_match($pattern, $this->getContainer()->request->get('_controller'), $matches);
        return strtolower($matches[1]);
    }

    private function _getActionName()
    {
        $pattern = "#::([a-zA-Z]*)Action#";
        $matches = array();
        preg_match($pattern, $this->getContainer()->request->get('_controller'), $matches);
        return $matches[1];
    }

    public function defaultBodyClassesFunction($userClasses = NULL)
    {
        $classes = array();
        $classes[] = $this->_getControllerName();
        $classes[] = $this->_getActionName();
        if ($userClasses) {
            $classes[] = $userClasses;
        }
        return join(' ', $classes);
    }

    public function getName()
    {
        return 'ui_extensions';
    }
}

In my Twig file, when I call defaultBodyClasses(), Symfony is giving me an error: The function "defaultBodyClasses" does not exist and references the Twig file where I'm calling the function.

Any ideas what could be going on?

Thanks, Nate


EDIT:

Here's the service definition:

<parameters>
    <parameter key="myproject.twig.extensions.ui_extensions.class">MyProject\Bundle\MyBundle\Twig\Extensions\TwigUiExtensions</parameter>
</parameters>

<services>
    <service id="myproject.twig.extensions.ui_extensions" class="%myproject.twig.extensions.ui_extensions.class%">
        <call method="setContainer">
            <argument type="service" id="service_container" />
        </call>
    </service>
</services>
1
Can you post your service definition?Carlos Granados

1 Answers

1
votes

You forgot to specify that this service is a Twig extension:

<parameters>
    <parameter key="myproject.twig.extensions.ui_extensions.class">MyProject\Bundle\MyBundle\Twig\Extensions\TwigUiExtensions</parameter>
</parameters>

<services>
    <service id="myproject.twig.extensions.ui_extensions" class="%myproject.twig.extensions.ui_extensions.class%">
        <tag name="twig.extension" />
        <call method="setContainer">
            <argument type="service" id="service_container" />
        </call>
    </service>
</services>