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:
{# page.html.twig #}
{% extends 'partials/base.html.twig' %}
{% block content %}
<div class="container">
{% include "partials/some-content.html.twig" %}
</div>
{% endblock %}
{# 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 ?
paul
October 16, 2017, 9:09am
2
I will add this to the cookbook, but here is an explanation about how to add asset on a specific page.
Basically, you need to extend the block that has all your js and call parents.
Here is a link: https://github.com/paulmassen/grav-collection#add-a-css-or-js-file-on-a-specific-page
1 Like
Thanks for your answer.
I have to do the assets.addJs()
in page.html.twig
with your explanation, but if I have:
{# 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.twig
and 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 ?
I can add assets in markdown using the shortcode assets plugin . This seems very inelegant. Is there a better approach?