1

I'd like to only load some scripts for 1 specific page with the Url /projects/.

Currently I'm using:

{% if craft.app.request.segments|first == 'projects' %}
 // load scripts
{% endif %}

This works.

However, I also have pages with the Url /projects/project-name which should not load the script.

How can you perform a conditional test for just the Url /projects/ ?

Thanks

  • {% if craft.app.request.segments|last == 'projects' %} did the trick – David Flindall Jul 4 at 7:58
2

You could use either | last filter instead of first or, a combination of first and another conditional ( segments[1] is empty or similar)?

{% set segments = craft.app.request.segments %}
{% if segments | first == “projects” and segments[1] is empty %}
Load scripts
{% endif %}

On phone so ymmv

| improve this answer | |
  • You might also be able to just test craft.app.request.uri or something (path?) check the request docs? – billythekid Jul 4 at 0:22
1

You might check if projects is part of URL:

{% if 'projects' in craft.app.request.segments|split('/') %}
Load scripts
{% endif %}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.