extends in symfony from multiple children - symfony

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

Related

Including header in base.twig for all pages

It is something pretty simple to be done outside of twig but here I am not even sure it is possible. This is the case:
Have base.html.twig. header.html.twig, home.html.twig.
#home.html.twig
{% extends 'base.html.twig' %}
{% block body %}
<h1>MY HTML</h1>
{% endblock %}
#base.html.twig
....some html here
{% block header%}{% endblock %}
{% block body %}{% endblock %}
....some more html here
#header.html.twig
{% extends 'base.html.twig' %}
{% block header %}
some here things that have to shown on every page through base.html.twig
{% endblock %}
I think it is pretty straight forward scenario but still my header doesn't show anywhere. As I understood from the documentation it is not working because this is how blocks work. It renders the page i am calling from my controller (home.html.twig) and the extended by it (base.html.twig). But wont call the header as well. So! How should I call the header on every page ?
To add the header on all pages just put include see example
{% block header %}
{% include 'header.html.twig' %}
{% endblock %}

Adding page specific js using timber twig

I have a twig file that extends base
{% extends "base.twig" %}
{% block content %}
<div> all articles </div>
{% for blog in blog_articles %}
<div><i> {{blog.post_title }}</i></div>
{% endfor %}
{% endblock %}
base.twig has an include for my global js and in my article.twig above i want to be able to add template specific JS that will appear below the global js that is in an include in base.twig
Can this be done?
To answer my own question i did this
in the base.twig
{% include 'globaljs.twig' %}
{% block javascript %}{% endblock %}
Then in the article.twig where i call the javascript block i just add my page scripts. simple

How to pass a block to an included template?

The structure of my Twig files looks like this:
- "skeleton_main"
- includes "skeleteon_header"
- render "block content"
- "skeleteon_header" should render "block breadcrumb"
- "partial"
- extends "skeleton_main"
- fills "block breadcrumb"
Now I can output "block breadcrumb" in "sekeleton_main" but it isn't passed to "skeleton_header". How can I access and render the block from within the included template? I tried using {% include '' with {} %} but without luck.
# skeleton_main
{% include 'header' %}
{% block content %}{% endblock %}
# header
{% block breadcrumb %}{% endblock %}
# partial
{% extends 'skeleton_main' %}
{% block breadcrumb %} Breadcrumb {% endblock %}
{% block content %} Content {% endblock %}
Maybe there's something wrong with this approach?
You are using include which does not permit overriding blocks.
Is there a reason to use include instead of extend ?
Another solution would be to use embed which does the same function as include, but permits overriding blocks at the same time:
http://twig.sensiolabs.org/doc/tags/embed.html
I think you have a wrong approch.
You should define header as a block, not as a separate template.
{# skeleton_main #}
{% block header %}
{% block breadcrumb %}{% endblock %}
{% endblock %}
{% block content %}{% endblock %}
{# partial #}
{% extends 'skeleton_main' %}
{% block breadcrumb %} Breadcrumb {% endblock %}
{% block content %} Content {% endblock %}

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.

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