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

What is the best way to add a javascript to a modular

Started by Muut Archive 10 years ago · 1 replies · 643 views
10 years ago

I am wondering what is the best way to include a javascript in a modular template. For example, I am making a modular that is a Google Map. There are a couple javascript files that I have to add to the bottom of the page. I only want to javascript to be included if the Google Map modular is on the page. I added them into a block, but that just adds the javascripts twice. I know this should be obvious to me, but I am just not sure what the best way to tackle this is.

10 years ago

Best thing is to add it in the javascripts block of the top level modular.html.twig file like this:

TWIG
{% extends 'partials/base.html.twig' %}

{% block javascripts %}
    {{ parent() }}
    {% do assets.add('theme://js/your-custom-js.min.js') %}
{% endblock %}
...

Suggested topics

Topic Participants Replies Views Activity
Archive · by Deleted User, 9 years ago
0 1999 9 years ago
Archive · by Muut Archive, 9 years ago
2 1342 9 years ago
Archive · by Muut Archive, 9 years ago
2 4548 9 years ago
Archive · by Muut Archive, 9 years ago
1 3401 9 years ago
Archive · by Muut Archive, 9 years ago
3 1492 9 years ago