layout.twig
<main class="main">
{% block content %}{% endblock content %}
</main>
{% block footer %}{% endblock %}
page.twig
{% extends 'layout.twig' %}
{% block content %}
{#abc#}
{% block footer %}
{#123#}
{% endblock footer %}
{% endblock content %}
When I do this. Footer block renders twice. Once after content block and once the place it should be
NOTE: I simplifed the templates to make them more readable. The problem I see is I cannot use block inside another block. if I use it renders twice.
I have to use footer inside block content. I don't want to include extends and content in every page. Because it is same. I use something like below.
$tm['p'] = "{% extends '" . $layout . "' %}{% block content %}";
$tm['p'] .= $page;
$tm['p'] .= "{% endblock content %}";
$load = new \Twig_Loader_Array($tm);
$tw = new \Twig_Environment($load);
print $tw->render("p", $dat);
I think it's because you're nesting a previously define block insinde another previously defined one when overriding one. A block is usually used to define placement for output elements, so it's kind of wrong that you're placing footer inside content and where it should be actually means after content block, where you defined it in layout.twig, that's the right place according to your template files. If you need to override footer, you should simply do this outside of content block inpage.twig like this:.
{% extends 'layout.twig' %}
{% block footer %}
{#123#}
{% endblock footer %}
{% block content %}
{#abc#}
{% endblock content %}
Please note, the order of overridden blocks in page.twig is not important because you already made the decision about this in layout.twig.
Looking at your example, I think you want to place footer inside <main></main> not the content block. If so, you might need to modify layout.twig. Some changes like the following:
layout.twig
<main class="main">
{% block content %}{% endblock content %}
{% block footer %}{% endblock footer %}
</main>
Templates are useful when you want to design page layout and elements' placements, and later only change their content not their placements. So you should design your layout.twig according to your needs, and if you need an alternate layout, feel free to create an additional one. For example if you need two layouts, one with footer inside the content and one with footer outside the content.
And about not repeating extends and blocks (content as you pointed) in every page: In my opinion it is not a good idea to remove the extends tag from top of the page. It is good for reference, to know which page template is extending which layout. And what you are doing is writing template source code as PHP strings. This is not a good practice. I suggest you consider saving your templates in separate files like you did with layout.twig and page.twig and put all fixed content inside them. And if you find yourself repeating some code very often, then you might want to rethink about your templates structure because on of the main advantages of templates hierarchy is to avoid writing common code by reusing it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With