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.
Archive
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 %}
...
Log in to reply.
Suggested topics
| Topic | Participants | Replies | Views | Activity |
|---|---|---|---|---|
| 0 | 1336 | 9 years ago | ||
| 2 | 925 | 9 years ago | ||
| 2 | 4056 | 9 years ago | ||
| 1 | 2941 | 9 years ago | ||
| 3 | 1112 | 9 years ago |