<p>You can filter Matrix blocks on field values – in your case, the field "grouping":</p>

<pre><code>{% for sponsor in sponsors.sponsors.grouping( 'Left column' ) %}
</code></pre>

<p>To conditonally output the whole wrapping div and section heading for the left and right aligned sponsors respectively, you can sort the sponsors into two arrays (left and right) beforehand, and then check the <code>length</code> property of those arrays before outputting the relevant markup.</p>

<p>Full example:</p>

<pre><code>{# Pull all sponsors from the Matrix field #}
{% set sponsors = entry.sponsors.type( 'sponsors' ) %}

{# Filter sponsors into two arrays by "grouping" field value #}
{% set sponsorsLeft = sponsors.grouping( 'Left column' ) %}
{% set sponsorsRight = sponsors.grouping( 'Right column' ) %}

{# I can haz sponsors in the left column? #}
{% if sponsorsLeft | length %}
    &lt;div class='left'&gt;
    &lt;h2&gt;LEFT COLUMN TITLE&lt;/h2&gt;
    {% for sponsor in sponsorsLeft %}
        &lt;li&gt;
            &lt;a href="{{ sponsor.sponsorUrl }}"&gt;{{ sponsor.sponsorName }}&lt;/a&gt;
        &lt;/li&gt;
    {% endfor %}
    &lt;/div&gt;
{% endif %}
...
{# I can haz sponsors in the right column? #}
{% if sponsorsRight | length %}
    &lt;div class='right'&gt;
    &lt;h2&gt;RIGHT COLUMN TITLE&lt;/h2&gt;
    {% for sponsor in sponsorsRight %}
        &lt;li&gt;
            &lt;a href="{{ sponsor.sponsorUrl }}"&gt;{{ sponsor.sponsorName }}&lt;/a&gt;
        &lt;/li&gt;
    {% endfor %}
    &lt;/div&gt;
{% endif %}
</code></pre>

CC BY-SA 3.0
2015-03-30T10:28:18.863Z
2015-03-30T10:50:07.313Z
2015-03-30T10:50:07.313Z
Mats Mikkel Rummelhoff
<p>You can filter Matrix blocks by setting a custom field parameter on the block query.</p>

<p>If I am right and in your example code <code>sponsors</code> is both,
your entry model and also the handle for your Matrix field, then this should do it.</p>

<pre><code>{% if sponsors.sponsors.grouping('Left column')|length %}
    &lt;div class="left"&gt;
        ...
    &lt;/div&gt;
{% endif %}
</code></pre>

CC BY-SA 3.0
2015-03-30T10:37:35.327Z
2015-03-30T10:37:35.327Z
carlcs
<p>I have a scenario where I'm using a Matrix to store and output 'sponsored links' for a page. Each link also has a radio button to select whether to display in a left hand column or a right hand column group.</p>

<p>Filtering like this is simple enough:</p>

<pre><code>&lt;div class='left'&gt;
&lt;h2&gt;LEFT COLUMN TITLE&lt;/h2&gt;
{% for sponsor in sponsors.sponsors %}
    {% if sponsor.grouping == "Left column" %}
        &lt;li&gt;
            &lt;a href="{{ sponsor.sponsorUrl }}"&gt;&lt;img src="{{ sponsor.sponsorLogo.first().getUrl('maxwidth220') }}" alt="{{ sponsor.sponsorName }}" /&gt;&lt;/a&gt;
        &lt;/li&gt;
    {% endif %}
{% endfor %}
&lt;/div&gt;
...
&lt;div class='right'&gt;
&lt;h2&gt;RIGHT COLUMN TITLE&lt;/h2&gt;
{% for sponsor in sponsors.sponsors %}
    {% if sponsor.grouping == "Right column" %}
        &lt;li&gt;
            &lt;a href="{{ sponsor.sponsorUrl }}"&gt;&lt;img src="{{ sponsor.sponsorLogo.first().getUrl('maxwidth220') }}" alt="{{ sponsor.sponsorName }}" /&gt;&lt;/a&gt;
        &lt;/li&gt;
    {% endif %}
{% endfor %}
&lt;/div&gt;
</code></pre>

<p>This works, but it will always spit out a wrapping div and section heading, even if there is no content. How do I test to see if there's anything to spit out first?</p>

<templating><matrix>
Only execute twig block if a Matrix radio has a certain value
# Only execute twig block if a Matrix radio has a certain value

(Asked by: username_0 on 2015-03-30)

I have a scenario where I'm using a Matrix to store and output 'sponsored links' for a page. Each link also has a radio button to select whether to display in a left hand column or a right hand column group.
Filtering like this is simple enough:

LEFT COLUMN TITLE
{% for sponsor in sponsors.sponsors %}
    {% if sponsor.grouping == "Left column" %}
        
            
        
    {% endif %}
{% endfor %}

...

RIGHT COLUMN TITLE
{% for sponsor in sponsors.sponsors %}
    {% if sponsor.grouping == "Right column" %}
        
            
        
    {% endif %}
{% endfor %}


This works, but it will always spit out a wrapping div and section heading, even if there is no content. How do I test to see if there's anything to spit out first?

**Tags:** templating, matrix


---

## 2 answers

### Answer by username_1 on 2015-03-30. Score: 4 | Accepted answer

You can filter Matrix blocks on field values – in your case, the field "grouping":
{% for sponsor in sponsors.sponsors.grouping( 'Left column' ) %}

To conditonally output the whole wrapping div and section heading for the left and right aligned sponsors respectively, you can sort the sponsors into two arrays (left and right) beforehand, and then check the length property of those arrays before outputting the relevant markup.
Full example:
{# Pull all sponsors from the Matrix field #}
{% set sponsors = entry.sponsors.type( 'sponsors' ) %}

{# Filter sponsors into two arrays by "grouping" field value #}
{% set sponsorsLeft = sponsors.grouping( 'Left column' ) %}
{% set sponsorsRight = sponsors.grouping( 'Right column' ) %}

{# I can haz sponsors in the left column? #}
{% if sponsorsLeft | length %}
    
    LEFT COLUMN TITLE
    {% for sponsor in sponsorsLeft %}
        
            {{ sponsor.sponsorName }}
        
    {% endfor %}
    
{% endif %}
...
{# I can haz sponsors in the right column? #}
{% if sponsorsRight | length %}
    
    RIGHT COLUMN TITLE
    {% for sponsor in sponsorsRight %}
        
            {{ sponsor.sponsorName }}
        
    {% endfor %}
    
{% endif %}


---

### Answer by username_2 on 2015-03-30. Score: 3

You can filter Matrix blocks by setting a custom field parameter on the block query.
If I am right and in your example code sponsors is both,
your entry model and also the handle for your Matrix field, then this should do it.
{% if sponsors.sponsors.grouping('Left column')|length %}
    
        ...
    
{% endif %}


---

