TransWikia.com

Passing multiple arguments to a macro?

Craft CMS Asked by Stephenio on March 8, 2021

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 ?

One Answer

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>

Correct answer by dmatthams on March 8, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP