Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Twig Include with a block

Tags:

twig

symfony

I need include a twig file with a block, but this file will be added in differents places. The file is this:

HTML CODE BLABLA

{% block javascript %}
    <script>
        Code associated with the html
    </script>
{% endblock %}

With include, is inserted all but not respect order block. Also I can load template with "use" but I havent access to the vars :S

{% include 'file.html.twig' %} No respect block
{% use 'file.html.twig' %} cant access vars

How can I do it ?. (Sorry for my english)

like image 753
MGDSoftware Avatar asked Oct 01 '22 21:10

MGDSoftware


2 Answers

To override a block you have to use the extends function. Your template may extends the parent, so the javascript block from the sub-template will replace the one from the parent.

You cannot mix include and extend.

like image 142
maphe Avatar answered Oct 18 '22 23:10

maphe


Have you tried the embed tag?

The embed tag combines the behaviour of include and extends. It allows you to include another template's contents, just like include does. But it also allows you to override any block defined inside the included template, like when extending a template.

like image 43
Adambean Avatar answered Oct 19 '22 00:10

Adambean