Skip to content
● Meet Grav Guard: keep every Grav site you run secure and up to date, all from one place. Free for up to 3 sites. Read more →

Add assets on Twig include

Started by Kubenexion 9 years ago · 3 replies · 1691 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 TomW, 2 weeks ago
8 227 8 hours ago
Support · by alex, 2 months ago
8 667 23 hours ago
Support · by Supple, 5 days ago
1 98 1 day ago
Support · by TomW, 1 week ago
3 159 3 days ago
Support · by TomW, 6 days ago
2 106 5 days ago