0

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?

Community
  • 1
  • 1
azzy81
  • 2,261
  • 2
  • 26
  • 37
  • Are sure to have the correct version of TwigBridge? https://github.com/symfony/symfony/blob/v2.3.3/src/Symfony/Bridge/Twig/Extension/HttpKernelExtension.php#L41 – dezull Aug 20 '13 at 13:46
  • And the right version of SF? – mmoreram Aug 20 '13 at 15:09
  • Thank you for your help. Twig Bridge is definitely installed even though I cant see a version number in the file however my version is 89 lines the same as the one you linked to. I havnt got a 'use' statement at the top of my controller thought which makes me think I need to include the class namespace in my controller. symfony is version 2.3.3 installed via composer and everything else works a treat. – azzy81 Aug 20 '13 at 15:47
  • Did you start with Symfony Framework Standard edition or did you "manually" make your project using Symfony Framework? You could try downgrading to 2.3.2 (using composer). – hacfi Aug 21 '13 at 17:25

1 Answers1

0

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.

azzy81
  • 2,261
  • 2
  • 26
  • 37