1

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') %}

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

2 Answers 2

2

If I understand you correctly this should do the job:

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

.

Also check out Twig docs:

3
  • Awesome thanks that's exactly what I couldn't find how to do.
    – julzmon
    Jun 26, 2014 at 20:27
  • Is the "> 0" necessary? I though usually just the length will work because it would be 0?
    – julzmon
    Jun 27, 2014 at 1:08
  • That would work just as well, I just wanted to make it as clear as possible.
    – Victor
    Jun 27, 2014 at 9:04
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  %}
    <button class="filt btn" type="button" data-filter=".CD">CD</button>
{% endif %}
3
  • Great thanks for help. I just tried this and it gave me an error "The merge filter only works with arrays or hashes."
    – julzmon
    Jun 27, 2014 at 1:03
  • What if you need to check more than 2 entries?
    – julzmon
    Jun 27, 2014 at 1:05
  • 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
    Jun 27, 2014 at 9:19

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.