Layout and Template Customization

Everything works, but if you go to /register... it looks awful. Well, of course it looks awful! FOSUserBundle has no idea how the page should be styled. But don't worry: we can get this looking much better, very quickly.

First, on the web debug toolbar, find the template icon and click it. This will show you all the templates used to render this page... which is a beautiful cheat sheet for knowing what templates you can override!

Correcting the Base Layout

The one I'm interested in is layout.html.twig, which lives in FOSUserBundle.

In my editor, I'll press Shift+Shift to open that file. Ok, every Twig template in FOSUserBundle extends this layout.html.twig file. For example, see the "Logged in as" text? That's coming from here.

But, we want all of FOSUserBundle's templates to instead extend our base.html.twig template. How can we do that?

Overriding the Layout

By overriding layout.html.twig. Let's see how. First, to override any template from a bundle, just go to app/Resources, then create a directory with the same name as the bundle: FOSUserBundle. Inside, create one more directory: views.


The location where templates should live to override bundle templates has changed in Symfony 4. But, the idea is still the same. For details, see:

In this case, the layout.html.twig template lives right the root of the views/ directory in the bundle. So that's where we need to create our's. Inside, extend the normal base.html.twig.

6 lines app/Resources/FOSUserBundle/views/layout.html.twig
{% extends 'base.html.twig' %}
... lines 2 - 6

Here's the magic part. Hit Shift+Shift again and open register.html.twig: this is the template for the registration page. Notice that it overrides a block called fos_user_content. In layout.html.twig, this is printed in the middle.

So check this out: inside of our version of layout.html.twig, add {% block body %}: that's the name of the block in our base.html.twig. Then, include {% block fos_user_content %} and {% endblock %}.

6 lines app/Resources/FOSUserBundle/views/layout.html.twig
{% extends 'base.html.twig' %}
{% block body %}
{% block fos_user_content %}{% endblock %}
{% endblock %}

We're effectively transferring the content from the fos_user_content block to the correct block: body.

These 5 lines of code are huge. Refresh the page! Ha! So much better! Not perfect, but every page now lives in our layout. If you want, you can even add a little more markup around the block.

12 lines app/Resources/FOSUserBundle/views/layout.html.twig
... lines 1 - 2
{% block body %}
<div class="container">
<div class="row">
<div class="col-xs-12">
{% block fos_user_content %}{% endblock %}
{% endblock %}

Overriding Individual Templates

Overriding the base layout is step one. But, each individual page still won't look quite right. On this page, we at least need a "Register" h1, and I'd like to make that button look better.

So in addition to overriding layout.html.twig, you really need to override every template from FOSUserBundle that you use - like registration, reset password, login and a few others.

Once again, click the templates link in the web debug toolbar. The template behind this page is register.html.twig, which we already have open. But notice, it immediately includes register_content.html.twig. This is a really common pattern in this bundle.

Let me show you: I'll click the views link to move my tree into FOSUserBundle. In Registration, we have register.html.twig and register_content.html.twig. In Profile there's the same for edit and show.

In most cases, you'll want to override the _content.html.twig template. Why? Well, it doesn't really matter: by overriding the _content.html.twig template, you don't need to worry about extending anything: you can just focus on the content.

Copy the contents of register_content.html.twig. Then, back in app/Resources/views, create a Registration directory. I'm doing that because this template lives in a Registration directory. Finally, create register_content.html.twig and paste in the content. Let's add a couple of classes to the button and an h1 that says: "Register Aquanauts!"

11 lines app/Resources/FOSUserBundle/views/Registration/register_content.html.twig
{% trans_default_domain 'FOSUserBundle' %}
<h1>Register Aquanaut!</h1>
{{ form_start(form, {'method': 'post', 'action': path('fos_user_registration_register'), 'attr': {'class': 'fos_user_registration_register'}}) }}
{{ form_widget(form) }}
<input class="btn btn-primary" type="submit" value="{{ 'registration.submit'|trans }}" />
{{ form_end(form) }}

Ok, refresh! Love it! In your app, make sure to do this for all of the different pages from the bundle that you're using. And remember, if you don't need a page - like the edit profile page - save yourself some time by not importing its route or overriding its template.

Leave a comment!

  • 2018-01-31 weaverryan

    Hey kaizoku!

    Sorry for the slow reply - Disqus put your comment in Spam! Rude! And yea, you're right, in Symfony 4, the location for putting your templates for overriding has changed. I'll add a ticket to add a note about this in the tutorial :).


  • 2018-01-27 kaizoku

    Hi guys, this isn't working anymore for SF4.
    Check this link to override templates :


  • 2017-09-06 Victor Bocharsky

    Hey Pascal,

    I think you can, create a new action, e.g. "/login-or-register", where create login and register forms, then render them in your own template. I think it should work out of the box as far as its URLs in form action attribute refers to the proper FOSUserBundle controllers, but probably if your form invalid - user will be on its specific form page. Otherwise, you need to handle those submitted forms manually, though you can steal ready code from FOSUserBundle but to do so probably it will be harder to upgrade FOSUserBundle in the future.


  • 2017-09-05 Pascal Q (pascal.)

    How can I use the login form in another template (ie include that login form to my own template which includes both register and login in one template) ?

  • 2017-08-11 mattxtlm

    Hey Diego Aguiar,

    that solved the issue totally. My translator in config.yml was deactivated. That could have been quite a long search, because I had no idea where to start.

    Thanks a lot!


    PS: I already tried to override the translation files, but that didn't have any effect. Now I know why :-)

  • 2017-08-11 Diego Aguiar

    Hey mattxtlm

    Those labels are made for beign translated, FOSUserBundle comes with translations for a lot of languages, but you have to activate it, like this:

    // config.yml
    translator: { fallback: en }

    if you give a look into the default form templates, you can see `{% trans_default_domain 'FOSUserBundle' %}` that's like the namespace where the FOS translations lives, if you would like to override them, you will have to create a translation file: `app/Resources/translations/FOSUserBundle.{language}.yml` and replace the key you want, but don't forget you need to clear cache whenever you create a new translation file (if you only update it, dont have to)

    I hope it helps you, cheers!

  • 2017-08-11 mattxtlm

    Somehow, my FOS forms (the customized and the non-customized) display labels like "" "form.password", "form.submit". It seems to me, some variables can't be resolved, but I don't know where to look at.

    Oh, and in my FOS Flash Messages and emails, that's also the case.


  • 2017-08-09 Diego Aguiar

    Hey kaizoku!

    If you are going to only override the templates, there are other easier ways to do it. I would suggest you to create a child bundle only when you don't have any other choice (maintaining extra bundles represents more work)

    Have a nice day :)

  • 2017-08-09 kaizoku

    It isn't recommended anymore to declare our Userbundle as child bundle of FOS in order to override template ?