gantry / gantry5

:rocket: Next Generation Template / Theme Framework
http://gantry.org
1.03k stars 206 forks source link

AddScript duplicate between particle and CSS/JS atom #3152

Open jmshey90 opened 1 year ago

jmshey90 commented 1 year ago

I'm using the CSS/JS atom to add a script: - { location: 'gantry-assets://js/eryc-datepicker.js', inline: '', in_footer: '1', extra: { }, priority: '1', contains, name: 'Date Picker' }

I also have a particle which loads it:

{% spaceless %}
{% do gantry.document.addScript(url('gantry-theme://js/eryc-datepicker.js'), 1, 'footer') %}
{% endspaceless %}

However, I get the script loaded twice. My understanding is that Gantry would only ever add one instance of the same script?

webarion commented 1 year ago

It's better not to mix atom and particle logic in the same file. But if you really want to, then you can try to do this in a particle:

{% set is_my_script = false %}

{% for atom in gantry.config.page.head.atoms if atom.type == 'assets' %}
  {% set attr = atom.attributes %}
  {% if attr.enabled %}
    {% for script in attr.javascript if script.location == 'gantry-assets://js/eryc-datepicker.js' %}
      {% set is_my_script = true %}
    {% endfor %}
  {% endif %}
{% endfor %}

{% if not is_my_script %}
  {% spaceless %}
  {% do gantry.document.addScript(url('gantry-assets://js/eryc-datepicker.js'), 1, 'footer') %}
  {% endspaceless %}
{% endif %}

Just an example. Perhaps there is a more elegant solution.