2

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:

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

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?

4

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 %}
    <div class='left'>
    <h2>LEFT COLUMN TITLE</h2>
    {% for sponsor in sponsorsLeft %}
        <li>
            <a href="{{ sponsor.sponsorUrl }}">{{ sponsor.sponsorName }}</a>
        </li>
    {% endfor %}
    </div>
{% endif %}
...
{# I can haz sponsors in the right column? #}
{% if sponsorsRight | length %}
    <div class='right'>
    <h2>RIGHT COLUMN TITLE</h2>
    {% for sponsor in sponsorsRight %}
        <li>
            <a href="{{ sponsor.sponsorUrl }}">{{ sponsor.sponsorName }}</a>
        </li>
    {% endfor %}
    </div>
{% endif %}
| improve this answer | |
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 %}
    <div class="left">
        ...
    </div>
{% endif %}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.