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

1
  • {% if craft.app.request.segments|last == 'projects' %} did the trick Jul 4, 2020 at 7:58

2 Answers 2

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

1
  • You might also be able to just test craft.app.request.uri or something (path?) check the request docs? Jul 4, 2020 at 0:22
1

You might check if projects is part of URL:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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