Twig : Included template extends parent Javascript bloc?

I am coding a project on Symfony, and for a page I need to split into several templates that I include in the main template.

Imagine this configuration:

base.html.twig

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        {% block stylesheets %}{% endblock %}

    </head>

    <body>
        {% block body %}{% endblock %}

        {{ encore_entry_script_tags('js/app') }} //like Jquery

        {% block javascripts %}{% endblock %}
    </body>
</html>

template1.html.twig

{% extends "base.html.twig" %}

#template1.html.twig
{% block body %}
    Some content

    {% include "template2.html.twig" %}
{% endblock %}

{% block javascripts %}

    {# Some scripts . . . #}
{% endblock %}

template2.html.twig :

#template2.html.twig
{% block body %}
    Some content
{% endblock %}

{% block javascripts %}

    {# Some scripts using Jquery . . . #}
{% endblock %}

In this configuration, I cannot use Jquery in the template2.html.twig, even if I imported it as a CDN in the base.html.twig template.

So how do you get template 2 to inherit the script imports made in base.html.twig ?

This is only a simple example here.

The real problem I face is similar.
I have in my base.html.twig all my compulsory imports which are transmitted to the child templates, and it works.

On the other hand, if my templates which extend the base.html.twig also include other templates, then I can no longer use the scripts imported into my base.html.twig

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *