3
votes

I tried to render a controller inside twig. I follow the documentation "Embedding other Controllers".

I constantly get an exeption with the method render(controller()): An exception has been thrown during the rendering of a template ("")

In my template.html.twig

{{ render(controller('MyBundle:User:logo')) }}

In my controller

class UserController extends Controller {
    public function logoAction()
    {        
        return $this->render('MyBundle:User:logo.html.twig', []);   
    }
}

It work well if I use in my template

{% render url('logo') %}

With the route

logo:
    pattern:  /logo
    defaults: { _controller: MyBundle:User:logo }

But I want to use render(controller())

What do I do wrong?

Edit, add more info about exception:

An exception has been thrown during the rendering of a template ("") in MyBundle:My/Dashboard:dashboard.html.twig at line 51.

500 Internal Server Error - Twig_Error_Runtime 1 linked Exception: ResourceNotFoundException ยป

[2/2] Twig_Error_Runtime: An exception has been thrown during the rendering of a template ("") in MyBundle:My/Dashboard:dashboard.html.twig at line 51.   -

in app/cache/dev/classes.php at line 7378   - } catch (Twig_Error $e) { throw $e; } catch (Exception $e) { throw new Twig_Error_Runtime(sprintf('An exception has been thrown during the rendering of a template ("%s").', $e->getMessage()), -1, $template->getTemplateName(), $e); } } elseif (false !== $parent = $this->getParent($context)) { $parent->displayBlock($name, $context, array_merge($this->blocks, $blocks), false); at Twig_Template
->displayBlock ('body', array('preset' => array('date' => '2015-03-31'),... 1/2] ResourceNotFoundException:    -

in app/cache/dev/appDevUrlMatcher.php at line 632   -
        }
        throw 0 < count($allow) ? new MethodNotAllowedException(array_unique($allow)) : new ResourceNotFoundException();
    } } at appDevUrlMatcher ->match ('/_fragment')  in app/cache/dev/classes.php at line 3278   + at Router ->match ('/_fragment')

Edit2 dashboard.html.twig

{% block body %}
    <div class="panel panel-default">
      <div class="panel-heading"> 
        <span class="glyphicon glyphicon-random" aria-hidden="true"></span>
        <b>{{ "Request"|trans }}</b>
      </div>
      <div class="panel-body">
          <form role="form" action="{{ path('dashboard', {'idDbd': idDbd}) }}">
                <div class="form-inline">
                    {{ render(controller('MyBundle:User:logo')) }}
                </div>
                {% include 'MyBundle:My/Request:request_edit.html.twig' %}
          </form>
      </div>
    </div>      
    (...)
1
what is your actual template path ? e.g /src/MyBundle/Resources/views/User/logo.html.twig - Heah
/src/CompanyModule/MyBundle/Resources/views/User/logo.html.twig - Alexandre
@Alexandre can you please give the second exception. That one actually explains what's going wrong. Or maybe the stacktrace of the exception? - Wouter J
@WouterJ done in the stack description... thanks for your help ;-) - Alexandre
from your stracktrace seems the problem is elsewhere, can you post the Dashboard:dashboard.html.twig files indicating the line 51? - Matteo

1 Answers

0
votes

To be able to use the controller() notation for subrequests, you need to enable the special FragmentListener which will be able to handle such subrequests.

You need to have the config for it in your config.yml file. This is done by default in new Symfony project: https://github.com/symfony/symfony-standard/blob/7311e7065bea8986f8eb2e95ab9dd4a55ce95ae2/app/config/config.yml#L31 Check whether you have this line in your own project