title: Embedding Controller using Twig
body: <p>I've made it simpler (apologies for the complicated question which I've left at the bottom).</p>
  
  <p>I want to have a twig template render another controller as a sub-part.</p>
  
  <p>Here's the parent twig (Resources/views/Default/testRenderParent.html.twig):</p>
  
  <pre><code>&lt;p&gt;Look! I am your father!&lt;/p&gt;
  &lt;p&gt;But look, I am not your
  {{ render(controller("SarelTestBundle:Default:testRenderChild")) }}&lt;/p&gt;
  </code></pre>
  
  <p>Here's the child twig (Resources/views/Default/testRenderChild.html.twig):</p>
  
  <pre><code>KID
  </code></pre>
  
  <p>The controller (Controller/DefaultController.php):</p>
  
  <pre><code>&lt;?php
  namespace Sarel\Test\TestBundle\Controller;
  use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  
  /**
   * @Template()
   **/
  class DefaultController extends Controller
  {
      /**
       * @Route("/testRenderParent")
       */
      public function testRenderParentAction()
      {
          return array();
      }
  
      /**
       * @Route("/testRenderChild")
       */
      public function testRenderChildAction() {
          return array();
      }
  }
  </code></pre>
  
  <p>When you run this, with the following URL /testRenderChild you should get "KID" echoed.
  When you run this, with the URL /testRenderParent (notice the "Parent" part), you should get echoed:</p>
  
  <p><em>Look! I am your father!<br>
  But look, I am your KID</em></p>
  
  <p>However, instead you get this echoed, and the error below it:</p>
  
  <p><em>Look! I am your father!<br>
  But look, I am not your<br>
  Look! I am your father!<br>
  But look, I am not your<br>
  Look! I am your father!<br>
  But look, I am not your<br>
  Look! I am your father!<br>
  But look, I am not your<br>
  Look! I am your father!<br>
  But look, I am not your<br>
  Look! I am your father!<br>
  But look, I am not your<br>
  Look! I am your father!<br>
  But look, I am not your<br>
  Look! I am your father!<br>
  But look, I am not your<br>
  FatalErrorException: Error: Maximum function nesting level of '250' reached, aborting! in    /Users/sarel/www/playpen/app/cache/dev/classes.php line 6134</em></p>
  
  <p>Thus you can see, it's rendering "Look! I am your father! But look, I am your "... and then instead of rendering the child controller with it's twig template, it re-renders the parent, ending in an endless loop, which is arrested by the exception.</p>
  
  <p><strong>--- copy before I changed it ---<br>
  --- copy before I changed it ---<br>
  --- copy before I changed it ---</strong>  </p>
  
  <p>So I'm trying to embed a controller (and ultimately the template that the action renders) in my symfony2 project using the guidelines in <a href="http://symfony.com/doc/current/book/templating.html#embedding-controllers" rel="nofollow">enter link description here</a>.</p>
  
  <p>My Controller is defined below:</p>
  
  <pre><code>&lt;?php
  namespace OTS\CoreBundle\Controller;
  
  /**
   * @Template()
   */
  class EmergencyContactsController extends Controller
  {
      public function addEmergencyContactAction(Request $request, $id) {
          return array();
      }
  }
  </code></pre>
  
  <p>I have a twig template attached to that, which right now just have "hello world" in the form of a twig file called addEmergencyContact.html.twig.</p>
  
  <p>When I go to the URL for this, which is something like localhost/my_route_to_it it works perfectly fine and I see "hello world" on the screen.</p>
  
  <p>Now, according to <a href="http://symfony.com/doc/current/book/templating.html#embedding-controllers" rel="nofollow">this</a> I merely have to put the following twig command in:</p>
  
  <pre><code>{{ render(controller('OTSCoreBundle:EmergencyContacts:addEmergencyContact', {'id': 15})) }}
  </code></pre>
  
  <p>When I do this and I load the holding-route it appears to be trying to load the main route, and then load the main route again inside the spot where I render the child-route. I know this because it's telling me there's variables missing - variables that I'm only using in the main twig file.</p>
  
  <p>Yet, when I:</p>
  
  <ol>
  <li>Don't have the {{ render... }} in there, it works fine - i.e. it's not other code that's giving me the exception of missing variable</li>
  <li>Render only the child-route I get a nice "hello world" back - i.e. it's not the child twig or child controller that's buggy</li>
  </ol>