<p>Why don't you pass an array as a parameter?</p>

<pre><code>{% macro mobileNav([
    {url: '/1', title: 'Foo'},
    {url: '/2', title: 'Bar'},
    {url: '/3', title: 'FooBar'},
], {} ) %}
</code></pre>

<p>And in your macro..</p>

<pre><code>{% for item in items %}
    {% if item.title is defined and item.title is not empty and item.url is defined and item.title is not empty %}
         // ... output something
         &lt;a href="{{ item.url }}"&gt;{{ item.title }}&lt;/a&gt;
         {{ items|length &lt;= 3? 'less than 4' : 'more than 3 are passed' }}
    {% endif %}
{% endfor %}
</code></pre>

CC BY-SA 4.0
2018-07-05T20:46:54.837Z
That was the ticket! Thank you so much for the suggestion - that solved the problem.
dpayne
CC BY-SA 4.0
2018-07-05T17:41:28.823Z
2018-07-05T20:48:18.940Z
2018-07-05T20:48:18.940Z
Robin Schambach
<p>I have a macro that can take up to six parameters which serve to populate nav links. The parameters are basically grouped into twos, with someone needing to pass in both a link and a title to make a nav link.</p>

<p>Below is my current code, which works as intended, but is not very DRY.</p>

<pre><code>{% macro mobileNav(navLink1, navTitle1, options={}) %}

{% import _self as self %}

  {% set options = {
    navLink2: null,
    navTitle2: null,
    navLink3: null,
    navTitle3: null,
    navClass: '',
    navItemClass: ''

  }| merge(options) %}

  &lt;nav {% if options.navClass | length %}class="{{ options.navClass }}"{% endif %}&gt;

    &lt;ul&gt;

      {% if navLink1 | length %}
        &lt;li {% if options.navItemClass | length %}class="{{ options.navItemClass }}"{% endif %}&gt;
          &lt;a href="{{ navLink1 }}"&gt;{{ navTitle1 }}&lt;/a&gt;
        &lt;/li&gt;
      {% endif %}

      {% if options.navLink2 | length %}
        &lt;li {% if options.navItemClass | length %}class="{{ options.navItemClass }}"{% endif %}&gt;
          &lt;a href="{{ options.navLink2 }}"&gt;{{ options.navTitle2 }}&lt;/a&gt;
        &lt;/li&gt;
      {% endif %}

      {% if options.navLink3 | length %}
        &lt;li {% if options.navItemClass | length %}class="{{ options.navItemClass }}"{% endif %}&gt;
          &lt;a href="{{ options.navLink3 }}"&gt;{{ options.navTitle3 }}&lt;/a&gt;
        &lt;/li&gt;
      {% endif %}

    &lt;/ul&gt;

  &lt;/nav&gt;

{% endmacro %}
</code></pre>

<p>I would like to DRY it out and have one list element where the number associated with the parameter dynamically increases.</p>

<p>Something like this (with the flexibility to an argument being passed via options as well):</p>

<pre><code> {% if navLink(a number between 1 &amp; 3 that is passed in) | length %}
        &lt;li {% if options.navItemClass | length %}class="{{ options.navItemClass }}"{% endif %}&gt;
          &lt;a href="{{ navLink(a number between 1 &amp; 3 that is passed in) }}"&gt;{{ navTitle(a number between 1 &amp; 3 that is passed in) }}&lt;/a&gt;
        &lt;/li&gt;
      {% endif %}
</code></pre>

<p>I haven't been able to figure out how to make it work though. Any help would be appreciated. Thanks!</p>

<macro>
How to Update an If Rule Based on Different Parameters Passed to a Macro?
# How to Update an If Rule Based on Different Parameters Passed to a Macro?

(Asked by: username_0 on 2018-07-05)

I have a macro that can take up to six parameters which serve to populate nav links. The parameters are basically grouped into twos, with someone needing to pass in both a link and a title to make a nav link.
Below is my current code, which works as intended, but is not very DRY.
{% macro mobileNav(navLink1, navTitle1, options={}) %}

{% import _self as self %}

  {% set options = {
    navLink2: null,
    navTitle2: null,
    navLink3: null,
    navTitle3: null,
    navClass: '',
    navItemClass: ''

  }| merge(options) %}

  

    

      {% if navLink1 | length %}
        
          {{ navTitle1 }}
        
      {% endif %}

      {% if options.navLink2 | length %}
        
          {{ options.navTitle2 }}
        
      {% endif %}

      {% if options.navLink3 | length %}
        
          {{ options.navTitle3 }}
        
      {% endif %}

    

  

{% endmacro %}

I would like to DRY it out and have one list element where the number associated with the parameter dynamically increases.
Something like this (with the flexibility to an argument being passed via options as well):
 {% if navLink(a number between 1 & 3 that is passed in) | length %}
        
          {{ navTitle(a number between 1 & 3 that is passed in) }}
        
      {% endif %}

I haven't been able to figure out how to make it work though. Any help would be appreciated. Thanks!

**Tags:** macro


---

## 1 answers

### Answer by username_1 on 2018-07-05. Score: 2 | Accepted answer

Why don't you pass an array as a parameter?
{% macro mobileNav([
    {url: '/1', title: 'Foo'},
    {url: '/2', title: 'Bar'},
    {url: '/3', title: 'FooBar'},
], {} ) %}

And in your macro..
{% for item in items %}
    {% if item.title is defined and item.title is not empty and item.url is defined and item.title is not empty %}
         // ... output something
         {{ item.title }}
         {{ items|length <= 3? 'less than 4' : 'more than 3 are passed' }}
    {% endif %}
{% endfor %}

> **Comments:**
> **username_0**: That was the ticket! Thank you so much for the suggestion - that solved the problem.  

---

