david
david

Reputation: 67

Include a controller to get parameter in Twig

I would like to include a controller into my defaut layout (in app/) to get access to the parameters returned in the array. I don't want to render. I tried to use {{ render_hinclude(controller('L3O1ProjetBundle:Advert:index')) }} But I'm not sure I understand what it does...

Thanks for your help!

Upvotes: 0

Views: 161

Answers (2)

Evgeniy Kuzmin
Evgeniy Kuzmin

Reputation: 2462

It is little bit strange approach to get parameters from the controller without render. But if you really need to get some set of data inside Twig, then the best way will be create a twig extension that will return an array. And if you need same data be returned as the controller makes, better to rebuild the app's architecture: dedicate the controller logic to a service that will return needed values both controller/extension, inject the service to the controller and twig extension and returns data from the service.

Upvotes: 0

René Höhle
René Höhle

Reputation: 27285

Here is an example from the documentation.

{# app/Resources/views/base.html.twig #}

{# ... #}
<div id="sidebar">
    {{ render(controller(
        'AcmeArticleBundle:Article:recentArticles',
        { 'max': 3 }
    )) }}
</div>

That calls AcmeArticleBundle:Article:recentArticles so first the bundle then the controller and then the function and render the output from that function.

http://symfony.com/doc/current/book/templating.html

Upvotes: 1

Related Questions