Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-03-30T10:28:18.863Z
Answers.LastActivityDate: 2015-03-30T10:50:07.313Z
Answers.LastEditDate: 2015-03-30T10:50:07.313Z
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-03-30T10:37:35.327Z
Answers.LastActivityDate: 2015-03-30T10:37:35.327Z
Answers.UserDisplayName: carlcs
Body: <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>
  
Tags: <templating><matrix>
Title: Only execute twig block if a Matrix radio has a certain value
ThreadText: # 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:
  
  ```
  <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?
  
  **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 %}
      <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 %}
  
  ```
  
  
  ---
  
  ### 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 %}
      <div class="left">
          ...
      </div>
  {% endif %}
  
  ```
  
  
  ---
  
  