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 == 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 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!

Related Questions

Using a variable for a hash key

2  Asked on August 26, 2021


Freeform with project.yaml?

2  Asked on August 20, 2021 by urs


All URLs changed path after upgrading to 3.6?

0  Asked on August 14, 2021 by thurc1996


SVG with tag does not work

3  Asked on August 14, 2021


Need help: multiple request throttling

0  Asked on August 13, 2021 by antoine-maillard


Craft CSRF protection and Facebook Tab

2  Asked on August 12, 2021


Search results query text fields and tag fields

0  Asked on August 11, 2021 by danny-valle


Ask a Question

Get help from others!

© 2022 All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP, SolveDir