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 · 1256 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 54 11 hours ago
Support · by Anna, 3 days ago
2 60 14 hours ago
Support · by Justin Young, 15 hours ago
1 30 15 hours ago
Support · by Duc , 1 week ago
2 65 5 days ago
Support · by Colin Hume, 1 week ago
2 57 5 days ago