1

I have a link on a website that i'd like to set a class of active on, based on different segments.

{% macro is_acitve(segment) %}
    {% if craft.app.request.getSegment(2) == segment %}active{% endif %}
{% endmacro %}
<a href="/the-url" class="{{ macros.is_active('test1', 'test2', 'test3') }}">Link Title</a>

It's not working. How would I go about this ?

1 Answer 1

2

You might be better off sending the values as an array, then you will only need one parameter.

{# Change the macro to check for the value in an array #}
{% macro is_active(segments) %}
    {% if craft.app.request.getSegment(2) in segments %}active{% endif %}
{% endmacro %}
{# Send an array to the macro #}
<a href="/the-url" class="{{ macros.is_active(['test1', 'test2', 'test3']) }}">Link Title</a>

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.