Skip to content
Grav 2.1 is out: every page speaks Markdown. Read the announcement →

Add assets on Twig include

Started by Kubenexion 9 years ago · 3 replies · 1509 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 complanar, 6 days ago
6 148 22 hours ago
Support · by Rick Beebe, 6 days ago
2 105 3 days ago
Support · by blu3prince, 3 days ago
0 55 3 days ago
Support · by alex, 4 days ago
2 97 3 days ago
Support · by Marcel, 2 weeks ago
1 146 4 days ago