MGDSoftware
MGDSoftware

Reputation: 157

Twig Include with a block

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)

Upvotes: 6

Views: 3992

Answers (2)

Adambean
Adambean

Reputation: 1161

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.

Upvotes: 1

maphe
maphe

Reputation: 1931

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.

Upvotes: 3

Related Questions