Search code examples
phptwigsymfony-2.3

Symfony 2.3.3 Embedding Controller


Im trying to embed a controller inside a twig template. Its documented here: http://symfony.com/doc/current/quick_tour/the_view.html and have read various questions on stackoverflow about it like this one: How to insert a Controller in Twig with "render" in Symfony 2.2?

However, when I try to embed my controller i get an error: "The function 'controller' does not exist"

My twig template looks like this:

<div class="section">
    <div id="a" class="section_column">
        {{ render(controller('MySymfonyBundle:AccessPage:load', {'page_id':'1'})) }}
    </div>
</div>

The controller that is being embedded does some db selects etc then renders another view to include inside this template.

This is the full error:

array(
['message'] =>
'Uncaught PHP Exception Twig_Error_Syntax: "The function "controller" does not exist in "<div id="{{ identifier }}" class="section">
<div id="a" class="section_column">
{{ render(controller('ABCBundle:Site:grabPage', {'page_id':'1'})) }}
</div>
</div>" at line 3" at /www/vendor/twig/twig/lib/Twig/ExpressionParser.php line 555'
['context'] =>
array(

['exception'] =>
array(

)
)
)

this just doesnt seem to work =( Where am I going wrong?


Solution

  • The reason this was erroring is because I was using the twig string loader and not loading a twig file. rendering controllers inside twig content must not work from a string.

    The twig template was stored in a mysql database and being extracted with Propel 1.6 then passed to the twig string loader to render the html.