Overriding FOSUserBundle Login Form - symfony

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 %}

Related

symfony twig extends: 'only extend if'

In twig there is the 'extends' tag, as found here; http://twig.sensiolabs.org/doc/tags/extends.html#conditional-inheritance
Now what I wanna do is something along the lines of the following example from that page:
{% extends standalone ? "minimum.html" : "base.html" %}
But rather than having 2 templates to extend from, I just want to extend from a template if a specific condition is met.
Now I've tried things such as:
{% extends boolean ? "template.html.twig" : "" %}
and:
{% if boolean %}
{% extends "template.html.twig" %}
{% endif %}
but the former gives an error saying it cannot find a template (since "" obviously isnt a valid path), and the latter just doesn't appear to do anything at all (or rather, it loads for a while and ends up not showing anything)
I've tried some other approaches, but couldn't come up with anything, so figured I'd ask here if I might be missing something.
Thanks in advance for any replies :)
EDIT: To sum up my intent; I am wondering if I can tell my template to only extend if a certain condition is met, and otherwise skip the extend step. (if condition then extend else do nothing)
Twig files are generated into PHP classes.
The extends tag should be the first tag in the template, as:
the {% extends %} tag will be converted to the PHP extends so the child template will inherit from the parent template.
the {% if %} tag is generated as a PHP if, inside a method of the template class, so you can't use {% if %} to extend some class or not.
Anyway, you can extends a variable coming from your context, so you should put your condition in the controller.
if ($boolean) {
$template = 'hello.twig';
} else {
$template = 'world.twig';
}
$this->render("MyBundle:MyFeature:child.html.twig", array('template' => $template);
And then in child.html.twig:
{% extends template %}
I came with this hack: added empty layout only with content block. Seems to be working :) I can pass variable from controller and page is loaded with or without layout.
<!-- base.html.twig -->
<head>
...stuff...
</head>
<body>
{% block content %}{% endblock %}
</body>
<!-- empty.html.twig -->
{% block content %}{% endblock %}
<!-- some_page.html.twig -->
{% extends boolean ? 'base.html.twig' : 'empty.html.twig' %}
{% block content %}
Now this is my real content
{% endblock %}
In pure twig language, it could be something like this :
{% if app.request.pathinfo starts with '/react' %}
{% set extendPath = "::react_base.html.twig" %}
{% else %}
{% set extendPath = "CoreBundle::layout.html.twig" %}
{% endif %}
{% extends extendPath %}

Symfony2 how to integrate FOSUserBundle register form inside template

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.

Override form_end block for one form

I want to override form_end block but only for one form. My blocks are loaded globally in my config.yml file. For example when i want to override form_row for one field i can make block like this -> formName.field.row.
I was trying to make something like this:
{% block formName_form_end %}
my Code
{% endblock %}
but It doesn't work. Maybe it's just not possible to override form_end using form_name ? I mean that i can only override form_end, but in my case it will affect all forms.
You need form themeing.
{% form_theme form _self %}
{% block form_end %}
my Code
{% endblock %}
{# Rest of template #}
As noted in the documentation, use of _self tells Twig to look for block overrides within the current template file.

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 %}

Problems with include

i have a lot of time programming in PHP, but im doing my first steps in Symfony.
Im try write Twigg templates, i have a public template in app/Resources/view/public.html.twig.
This file contains: http://pastebin.com/T1KGMfXL.
Now, in CloudBundle, have a base.html.twig:
{% extends '::public.html.twig' %}
{% block main %}
<div class="login_page">
<div class="login_box">
{% block content %} {% endblock %}
</div>
</div>
{% endblock %}
And the content in another twig file. For example, login.html.twig
{% extends 'base.html.twig' %}
{% block content %}
....
{% endblock %}
In the Controller, when an user try http://cloud.man.local/app.php/login:
public function staticAction($sitio)
{
// in this case, $sitio contains "login"
return $this->render("CloudBundle:Default:$sitio.html.twig");
}
So, the problem is that, only shows the footer, not show the content.
Any ideas ?.
You want to override a block which is inside another block, so in your case I think you should try this in the another twig files:
{% block main %}
{{ parent() }}
{% block content %} YOur content {% endblock %}
{% endblock %}
see http://twig.sensiolabs.org/doc/functions/parent.html
Hope it's helpful.
Best regard.

Resources