<p>If I understand you correctly this should do the job:</p>

<pre><code>{% if albumCd|length &gt; 0 or singleCd|length &gt; 0  %}
    &lt;button class="filt btn" type="button" data-filter=".CD"&gt;CD&lt;/button&gt;
{% endif %}
</code></pre>

<p>.</p>

<p>Also check out Twig docs:</p>

<ul>
<li><a href="http://twig.sensiolabs.org/doc/tags/if.html" rel="nofollow">if-statement</a></li>
<li><a href="http://twig.sensiolabs.org/doc/templates.html#comparisons" rel="nofollow">comparison operators</a></li>
<li><a href="http://twig.sensiolabs.org/doc/filters/index.html" rel="nofollow">Filters</a></li>
</ul>

CC BY-SA 3.0
2014-06-26T20:27:31.797Z
Awesome thanks that's exactly what I couldn't find how to do.
julzmon
CC BY-SA 3.0
2014-06-27T01:08:53.603Z
Is the "> 0" necessary? I though usually just the length will work because it would be 0?
julzmon
CC BY-SA 3.0
2014-06-27T09:04:18.280Z
That would work just as well, I just wanted to make it as clear as possible.
Victor
CC BY-SA 3.0
2014-06-26T19:44:22.687Z
2014-06-26T19:44:22.687Z
Victor
<p>Here's just a variation on the conditional that Victor provided that will do the same thing, might be a bit cleaner if you ever need to check several arrays:</p>

<pre class="lang-twig prettyprint-override"><code>{% if albumCd|merge(singleCd)|length  %}
    &lt;button class="filt btn" type="button" data-filter=".CD"&gt;CD&lt;/button&gt;
{% endif %}
</code></pre>

CC BY-SA 3.0
2014-06-27T01:03:46.603Z
Great thanks for help. I just tried this and it gave me an error "The merge filter only works with arrays or hashes."
julzmon
CC BY-SA 3.0
2014-06-27T01:05:59.203Z
What if you need to check more than 2 entries?
julzmon
CC BY-SA 3.0
2014-06-27T09:19:15.313Z
albumCd should be an array of entries according to your code above. to check more than two, just continue using the merge filter to merge them.
Ben Croker
CC BY-SA 3.0
2014-06-26T20:18:21.737Z
2014-06-27T16:26:51.670Z
2014-06-27T16:26:51.670Z
Ben Croker
<p>I'm creating an isotope filter and have the following code. Could someone please help me write an if statement showing the buttons only if any of these entries have content. For instance the CD button if statement would check if the Entry "album" type "cd" and Entry "single" type "cd".</p>

<pre><code>{# Albums #}
{% set albumCd = entries.section('album').type('cd') %}
{% set albumVinyl = entries.section('album').type('vinyl') %}
{% set albumCassette = entries.section('album').type('cassette') %}
{% set albumOther = entries.section('album').type('other') %}

{# Singles #}
{% set singleCd = entries.section('single').type('cd') %}
{% set singleVinyl = entries.section('single').type('vinyl') %}
{% set singleCassette = entries.section('single').type('cassette') %}
{% set singleOther = entries.section('single').type('other') %}

{# Collectable #}
{% set otherVideo = entries.section('other').type('video') %}
{% set otherMagazine = entries.section('other').type('magazine') %}
{% set otherPrint = entries.section('other').type('print') %}
{% set otherCollectable = entries.section('other').type('collectable') %}

&lt;button class="filt btn" type="button" data-filter="*"&gt;show all&lt;/button&gt;
&lt;button class="filt btn" type="button" data-filter=".CD"&gt;CD&lt;/button&gt;
&lt;button class="filt btn" type="button" data-filter=".Vinyl"&gt;Vinyl&lt;/button&gt;
&lt;button class="filt btn" type="button" data-filter=".Cassette"&gt;Cassette&lt;/button&gt;
&lt;button class="filt btn" type="button" data-filter=".Other"&gt;Other&lt;/button&gt;
&lt;button class="filt btn" type="button" data-filter=".Magazine"&gt;Magazine&lt;/button&gt;
&lt;button class="filt btn" type="button" data-filter=".Print"&gt;Print&lt;/button&gt;
&lt;button class="filt btn" type="button" data-filter=".Collectable"&gt;Collectable&lt;/button&gt;
</code></pre>

<templating>
How do I write an if statement to show Isotope filter button only if the Entry Section has content?
# How do I write an if statement to show Isotope filter button only if the Entry Section has content?

(Asked by: username_0 on 2014-06-26)

I'm creating an isotope filter and have the following code. Could someone please help me write an if statement showing the buttons only if any of these entries have content. For instance the CD button if statement would check if the Entry "album" type "cd" and Entry "single" type "cd".
{# Albums #}
{% set albumCd = entries.section('album').type('cd') %}
{% set albumVinyl = entries.section('album').type('vinyl') %}
{% set albumCassette = entries.section('album').type('cassette') %}
{% set albumOther = entries.section('album').type('other') %}

{# Singles #}
{% set singleCd = entries.section('single').type('cd') %}
{% set singleVinyl = entries.section('single').type('vinyl') %}
{% set singleCassette = entries.section('single').type('cassette') %}
{% set singleOther = entries.section('single').type('other') %}

{# Collectable #}
{% set otherVideo = entries.section('other').type('video') %}
{% set otherMagazine = entries.section('other').type('magazine') %}
{% set otherPrint = entries.section('other').type('print') %}
{% set otherCollectable = entries.section('other').type('collectable') %}

show all
CD
Vinyl
Cassette
Other
Magazine
Print
Collectable

**Tags:** templating

---

## 2 answers

### Answer by username_1 on 2014-06-26. Score: 2 | Accepted answer

If I understand you correctly this should do the job:
{% if albumCd|length > 0 or singleCd|length > 0  %}
    CD
{% endif %}

.
Also check out Twig docs:

if-statement
comparison operators
Filters

> **Comments:**
> **username_0**: Awesome thanks that's exactly what I couldn't find how to do.  
> **username_0**: Is the "> 0" necessary? I though usually just the length will work because it would be 0?  
> **username_1**: That would work just as well, I just wanted to make it as clear as possible.  

---

### Answer by username_2 on 2014-06-26. Score: 3

Here's just a variation on the conditional that Victor provided that will do the same thing, might be a bit cleaner if you ever need to check several arrays:
{% if albumCd|merge(singleCd)|length  %}
    CD
{% endif %}

> **Comments:**
> **username_0**: Great thanks for help. I just tried this and it gave me an error "The merge filter only works with arrays or hashes."  
> **username_0**: What if you need to check more than 2 entries?  
> **username_2**: albumCd should be an array of entries according to your code above. to check more than two, just continue using the merge filter to merge them.  

---

