apfelbox
apfelbox

Reputation: 2634

Symfony2: How to render partial in Twig extension

I have a already working Twig extension in my Symfony2 app:

namespace Company\MyBundle\Service;

class MyExtension extends \Twig_Extension
{
    // ...
}

I now want to create a Twig function, which itselfs takes some data and renders a partial template. But my question is: how do I get a new templating instance in my twig extension service?

Here is my current config:

services:
    twig.extension.my_extensions:
        class: Company\MyBundle\Service\TwigExtension
        tags:
            - { name: twig.extension }

If I now add arguments: [@templating] to the config, I get an (understandable) circular reference exception.

Upvotes: 1

Views: 3709

Answers (1)

richsage
richsage

Reputation: 27102

It seems one of the recommended simple ways is to inject the container directly and retrieve the templating engine from there. As you've seen, injecting in the templating engine directly causes a circular reference exception.

So, inject in @service_container and you should be good. This seems to be the approach taken by bundles such as the FOSFacebookBundle as well.

Upvotes: 1

Related Questions