Symfony2 how to integrate FOSUserBundle register form inside template - symfony

i try to integrate the FOSUserBundle register form into my homepage (index.html.twig template. In my index.html.twig, i use the render function to show the form in my login block as follow :
{% block container %}
[...]
{% block login %}
{% render url('fos_user_registration_register') %}
{% endblock login %}
[...]
{% endblock container %}
All is ok at this point.But when i submit the form, with errors, the FOSUserBundle controler redirect to my overrided register template, and only the form is rendered, not all the page with the form inside. I tried to extend the overided register form to write into the login block
{% extends "ELSiteBundle:Site:index.html.twig" %}
{% block login %}
<div>
<form ...>
[my html form]
</form>
</div>
{% endblock login %}
It works when FOSUserBundle redirect to the register template, but now, when the homepage is called, i get a duplicate html content into the login block (it rewrites the whole page into login block).
So my question : How can i render the register.html.twig template into my login block, for both form (default, and with error) ?

You have to override the Resources/views/layout.html.twig template:
{% extends 'AcmeDemoBundle::layout.html.twig' %}
{% block title %}Acme Demo Application{% endblock %}
{% block content %}
{% block fos_user_content %}{% endblock %}
{% endblock %}
Overriding Default FOSUserBundle Templates
Then if you using {{ render(controller('FOSUserBundle:Registration:register')) }}
when errors occur you will be on register page with your common layout style.
If you using {% render url('fos_user_registration_register') %}
you can override Register controller, then trigger form submission with isSubmitted() and depend of that load template with or without {% extends "ELSiteBundle:Site:index.html.twig" %} part.

Related

extends in symfony from multiple children

I have a base.html.twig with the basic html.
In the 'base' I have a {% block body %}{% endblock %} and a {% block javascript %}{% endblock %}
I call a 'form.html.twig' file from the Controller and the template extends the base.html.twig.
The 'form' contains the <form> and </form> tags and can contain a random numbers of sub templates/form elements:
'{% block body %}
{% for template in templates %}
{% embed template %}{% endembed %}
{% endfor %}
{% endblock %}'
The 'template' is perhaps a customer.html.twig, confirm.html.twig, vehicle.html.twig etc. etc. and they all have {% block body %} and {% block javascript %} - now for the question:
The content in the template files aren't moved to the blocks body/javascript. How do I get twig/symfony to move the content in the template files to the respective areas in the 'base.html.twig'?
Thank you for your time.
You want to use the use twig tag instead of the embed tag like this
{% use template %}
See more info here:
http://twig.sensiolabs.org/doc/tags/use.html

Sonata Block - add javascript block

Is it possible to add a javascript block in a Sonata Block View like this ?
{% extends sonata_block.templates.block_base %}
{% block block %}
<!-- html -->
{% endblock %}
{% block javascripts %}
// javascript code
{% endblock %}
I couldn't make it works in this way as javascript block doesn't exist in template block base but present in my layout.

How to add custom content in child template in Symfony2?

I have a index.html.twig page as my homepage with extend
{% extends "::base.html.twig" %}
pointing to app/Resources/Views base template and my that base template extends a
{% extends "UAMBootstrapBundle::stickyfooter.html.twig" %}
How I can add some custom content to get displayed on my homepage template index.html.twig? Whenever I add contents the error is that I can't any body to it if it has extends.
You get that error if forget to use the {% block %}{% endblock %}
So you need to create a block in you base.html.twig which you can 'fill' in your index.html.twig
Example in base.html.twig
{% block content %}{% endblock %}
In your index.html.twig
{% block %}My content{% endblock %}

Overriding FOSUserBundle Login Form

Im following the documentation here:https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/doc/overriding_templates.rst
I chose to Create A Child Bundle And Override Template so in my bundle I have
class MyBundle extends Bundle
{
//declare bundle as a child of the FOSUserBundle so we can override the parent bundle's templates
public function getParent()
{
return 'FOSUserBundle';
}
}
In my bundle I have added the following files
MyBundle
\Resources
\views
\Security
login.html.twig
Matching the FOS bundle structure as mentioned in the documentation
login.html.twig
{% extends 'AnotherBundle::layout.html.twig' %}
{% block title %}Log In{% endblock %}
{% block content %}
{% block fos_user_content %}{% endblock %}
{% endblock %}
When I go to the login page my header loads fine but there's no login form, what am doing wrong?
Because you didn't write the code that render the login form.
open /vendor/friendsofsymfony/user-bundle/FOS/UserBundle/Resources/views/Security/login.html.twig, copy the code in the fos_user_content block to your custom login.html.twig, reload the page, then you'll see the form.
If you want to customize the form, rewrite the code you've copied.
When you have nested blocks you need to tell explicitly which block you're closing. So, try this:
{% block content %}
{% block fos_user_content %}{% endblock fos_user_content %}
{% endblock content %}

Symfony2 inherit from two form templates

I've set my forms to use a custom template (that produces a DL) and that works fine
twig:
form:
resources:
- 'myBundle:Form:row.html.twig'
But then I want to customise one row of one form and I think I have to do this
{% form_theme edit_form _self, ' %}
{% block _startYear_row %}
{% block form_row %}
{{ parent() }}
<div id="startCalendaryear"></div>
{% endblock %}
{% endblock %}
That make the entire form inherit from _self and loses the customisation of row.html.twig.
How can I set a custom template for all forms while still overriding a single row on one of them?

Resources