Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

Support

Add assets on Twig include

Started by Kubenexion 9 years ago · 3 replies · 1258 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 Thomas, 1 week ago
2 56 13 hours ago
Support · by Anna, 3 days ago
2 61 16 hours ago
Support · by Justin Young, 17 hours ago
1 32 17 hours ago
Support · by Duc , 1 week ago
2 66 5 days ago
Support · by Colin Hume, 1 week ago
2 59 5 days ago