Skip to content
● Grav 2.2 is out: 2x faster cold starts and 96% less memory on big sites. Read the announcement →

Add assets on Twig include

Started by Kubenexion 9 years ago · 3 replies · 1564 views
9 years ago

Hello,

I have a page using a Twig include to a partials/some-content.html.twig and in this file I want to load a specific js script.
I tried this way:

PHP
{# page.html.twig #}

{% extends 'partials/base.html.twig' %}

{% block content %}
    <div class="container">
        {% include "partials/some-content.html.twig" %}
    </div>
{% endblock %}
PHP
{# partials/some-content.html.twig #}

{% do assets.addJs("https://some.cdn/some-script.js") %}

<div class="some-content"></div>

But the script https://some.cdn/some-script.js is not added to the page.

Is there a special way to add the script in this case ?

9 years ago

Thanks for your answer.

I have to do the assets.addJs() in page.html.twig with your explanation, but if I have:

PHP
{# page.html.twig #}

{% extends 'partials/base.html.twig' %}

{% block content %}
    <div class="container">
        {% if condition %}
            {% include "partials/some-content1.html.twig" %}
        {% else%}
            {% include "partials/some-content2.html.twig" %}
        {% endif %}
    </div>
{% endblock %}

with partials/some-content1.html.twigand partials/some-content2.html.twig requiring 2 differents JS (not the same), how can I avoid to load useless JS ?
Did I have to duplicate the condition in page.html.twig in my JS block ?

Suggested topics

Topic Participants Replies Views Activity
Support · by webmasterodp, 1 week ago
4 157 6 days ago
Support · by nwhitfield, 1 week ago
2 135 1 week ago
Support · by stell, 3 weeks ago
5 369 2 weeks ago
Support · by gravinator, 1 month ago
5 559 2 weeks ago
Support · by Gravver, 3 weeks ago
6 347 3 weeks ago