FOSUserBundle: How to best integrate login and register form in one template?

tester picture tester · Jul 19, 2013 · Viewed 21.7k times · Source

I am using the FOSUserBundle in my Symfony application which is really great. They have the login and register forms in a separate template. I want both in one template to display them next to each other.

Therefore I created app/Resources/FOSUserBundle/Security/login.html.twig and app/Resources/FOSUserBundle/Registration/register.html.twig to override both templates. In login.html.twig I call the register controller to render its template.

app/Resources/FOSUserBundle/Security/login.html.twig:

{% extends "FOSUserBundle::layout.html.twig" %}

{% trans_default_domain 'FOSUserBundle' %}

{% block fos_user_content %}
    {% if error %}
        <div>{{ error|trans }}</div>
    {% endif %}

    <form action="{{ path("fos_user_security_check") }}" method="post">
        <input type="hidden" name="_csrf_token" value="{{ csrf_token }}" />

        <input type="text" id="username" name="_username" value="{{ last_username }}" placeholder="{{ 'security.login.username'|trans }}" required="required" />

        <input type="password" id="password" name="_password" placeholder="{{ 'security.login.password'|trans }}" required="required" />

        <input type="checkbox" id="remember_me" name="_remember_me" value="on" />
        <label for="remember_me">{{ 'security.login.remember_me'|trans }}</label>

        <input type="submit" id="_submit" name="_submit" value="{{ 'security.login.submit'|trans }}" />
    </form>

    {{ render(controller('FOSUserBundle:Registration:register')) }}
{% endblock fos_user_content %}

app/Resources/FOSUserBundle/Registration/register.html.twig:

{% include "FOSUserBundle:Registration:register_content.html.twig" %}

But then I get the following error:

FatalErrorException: Error: Maximum function nesting level of '100' reached, aborting! in /private/var/www/symfony/My_UserBundle/vendor/twig/twig/lib/Twig/Node/Expression/Array.php line 31

I don't know why I get this error message. If I look into the Security:login and Registration:register controller, the templates get rendered in different ways:

$this->renderLogin(...

and

$this->container->get('templating')->renderResponse('FOSUserBundle:Registration:register.html....

So basically I have two questions:

  • What does the error message mean and how can I solve it?
  • Maybe this approach is not ideal, is there a better solution for this?

Answer

qooplmao picture qooplmao · Jul 20, 2013

You could use (as @Sidali Hallak said)

{% render url('fos_user_registration_register') %}
{% render url('fos_user_security_login') %}

But use your own versions of the FOSUserBundle:Registration:register.html.twig and FOSUserBundle:Security:login.html.twig templates that don't extend FOSUserBundle::layout.html.twig