Live geek or die tryin'

Posted on by Dinduks


I discovered some days ago a secret Symfony 2's helper. I call it secret because I can't find it anywhere on the documentation.
Edit: Here it is on the Symfony2 book: Embedding Controllers

{% render %} is used to show an action's response in a Twig template. It's very useful when wanting to show a dynamic content in all the app's pages.

Example: Displaying online users' counter.

  • First of all we have to create a onlineUsersAction() function that fetches, processes the result and returns the counter of the online users.
  • Then we gotta create a Twig template that displays the response returned by the action. The syntax is the following:
    {% render "LiveGeekBundle:Default:onlineUsers" %}

    Knowing that onlineUsersAction() is an action of DefaultController which is a controller from the Live\GeekBundle bundle.

  • Finally we must include that template in our application's default layout, and thus display the online users' counter in all the app's pages.

Passing arguments:
You can pass arguments to the action this way:

  • In the Twig template:

    {% render "LiveGeekBundle:Default:onlineUsers" with { 'arg1' : 'value1', 'arg2' : 'value2' } %}
  • In the controller:

    helloAction($arg1, $arg2)
    

    Or

    helloAction($arg2, $arg1)
    

    The arguments' order doesn't matter.

Enjoy. ;)

Posted on by Dinduks | Posted in Development, Pieces of code, Symfony 2 | Tagged , , ,


5 Responses to Symfony2: “render” helper for calling an action from a view

  1. niko says:


    Very good solution, thanks!
    Is there any way to send additional arguments to the function?


    • Dinduks says:


      Hello,

      Yes it’s possible to send arguments:
      In your template: {% render ‘blabla’ with { ‘arg1′ : ‘value1′, ‘arg2′ : ‘value2′ } %}
      In your controller: helloAction($arg1, $arg2) or helloAction($arg2, $arg1)
      The arguments’ order doesn’t matter.

      I will update the post with this information. :)


  2. Adam S says:


    There is actually a section for embedding controls in the symfony2 book (available electronically on the official site):

    http://symfony.com/doc/2.0/book/templating.html#embedding-controllers

    Best of luck.


    • Dinduks says:


      Thanks for you comment.
      I have found this section also only yesterday. I think that it’s hard to find tho, especially that “Embedding Controllers” isn’t a pertinent title imo.


  3. Mauro says:


    I try it but the parameters of the request are always cached, if I pass app.user.id it render always the same page. For example in the layout I have this:
    {% render “MyBundle:Controller:Action” with { ‘userId’: app.user.id }, {‘standalone’: true} %}

    And always request the first cached page, from debug header:

    x-symfony-cache GET /: fresh; GET /_internal/MyBundle:Controller:Action/userId=1.html: fresh

    There is a way to not cache the request?


Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>