4

Is it possible to create multiple Twig extension ? Because when I add a second one, I get the error message that the first extension method is not existing in the second extension.

Here is how my extension are defined in services.xml :

<service id="acme.extension.view_helper" class="FLS\AcmeBundle\Extension\ViewHelperExtension">
        <argument type="service" id="doctrine.orm.entity_manager" />
        <tag name="twig.extension" />
 </service>

<service id="acme.extension.stats_helper" class="FLS\AcmeBundle\Extension\StatsExtension">
        <argument type="service" id="doctrine.orm.entity_manager" />
        <tag name="twig.extension" />
</service>

And here is the error message :

Fatal error: Call to undefined method FLS\AcmeBundle\Extension\StatsExtension::findTask() in F:\www\AcmeBundle\app\cache\dev\twig\fe\fd\4ff31bf8efd0669b6d0b2a14ba11.php on line 232

The findTask method is defined in ViewHelperExtension.

Thanks in advance!

3
  • can you share your ViewHelperExtension and StatsExtension code please Commented Jan 15, 2013 at 13:28
  • Same problem encountered here. The service definitions are Ok. Seems to be the case of a strange behavior: while defining two service extensions only the last defined one is available. This issue can be related to Twig Commented Feb 15, 2013 at 8:43
  • I have the same problem but only if the servises are in the same bundle Commented Mar 26, 2014 at 12:55

2 Answers 2

1

Edit: TL;DR. Yes. It is possible

I just had this issue right now. In my case, the reason for this was that I started with the same boilerplate for both extensions and the getName() function inside each was returning the same name.

Once I changed the name that function returns on each extension both extensions were added. I assume something like the following would work:

In src/AppBundle/Twig/AppBundleFooExtension.php

/* ... */
public function getName()
{
    return 'appbundle_foo_extension';
}
/* ... */

In src/AppBundle/Twig/AppBundleBarExtension.php

/* ... */
public function getName()
{
    return 'appbundle_bar_extension';
}
/* ... */
Sign up to request clarification or add additional context in comments.

Comments

0

I use several twig extension in my project and everything is ok. Your services.xml containes service tags? <services> // Services definition </services> Looks like second service override first.

Article How to write a custom Twig Extension

1 Comment

Strangely, everything I had to do in the services.xml file is to move the order of my services definitions. For example, I inserted between the two definitions of my extensions another service definition for a FormHandler and the override problem disappeared...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.