Including assets per page

Hi Everybody,
As in base.html.twig we add javascript assets like {% do assets.addJs(‘theme://js/something.js’) %} can we include it in modular.html.twig. We have modular design, and we need the assets to load only if the modular page is been used.

Thanks in advance

sure you can include it in modular. Assuming your base has has a structure like this:

    {% block javascripts %}
        {% do assets.add('jquery',102) %}
        {% do assets.addJs('theme://js/modernizr.custom.05229.js',101) %}
        {% do assets.addJs('theme://js/theme.js') %}
    {% endblock %}
    {{ assets.js() }}

You can override the javascript block, then call parent() to include the base stuff already defined, then add your own. Like this:

{% block javascripts %}
    {{ parent() }}
    {% do assets.add('theme://js/modular-only.js') %}
{% endblock %}

Of course, your modular twig page must be extending the base that has the javascript block defined.