<p>The proper conditional statement in this case would be to test for the <code>length</code> property of your Asset field, i.e. <em>cover</em>:</p>

<pre><code>{% if cover | length %}
</code></pre>

<p>The reason this works, is that calling <code>slide.coverImage</code> will – as long as the attribute "coverImage" exists for the "slide" model – never return a falsey value, even if there are no files. It will in fact always return an instance of the <a href="http://buildwithcraft.com/docs/templating/elementcriteriamodel" rel="nofollow">ElementCriteriaModel</a> class, i.e. an <em>object</em> representing your Asset field.</p>

<p>What the conditional <code>{% if cover | length %}</code> does is to read the <em>length</em> property of the ElementCriteriaModel object. The length property will represent the number of images in the Asset field – if it contains 0 images, the conditional will evaluate to "false" – as the number 0 within a conditional is interpreted as a falsey value. If you prefer, you can also write the statement like this:</p>

<pre><code>{% if cover | length &gt; 0 %}
</code></pre>

CC BY-SA 3.0
2015-03-17T22:42:40.923Z
2015-03-18T21:27:29.023Z
2015-03-18T21:27:29.023Z
Mats Mikkel Rummelhoff
<p>Here is my situation. This is not a whole code, only the part, where I'm having a problem. At the back end, model <code>coverbackup</code> is populated with files as well as model <code>cover</code>. If I depopulate <code>cover</code> (remove image from entry), I want to load model <code>coverbackup</code>. I was checking over and over, If I got to my file in <code>matrix</code> field correctly, but It seems I have. </p>

<p>Than I've tried to check if <code>elseif</code> statement loads anything on <code>cover</code>=<code>false</code>, <strong>I got empty output</strong>.</p>

<pre><code>{# Get the slides stored in variable "slides" #}
{% set slides = craft.entries.section('handle').type(['article', 'blog', 'news']).order('RAND()').limit(3) %}

    {% if slides %}
    &lt;ul class="uk-slideshow"&gt;

        {% for slide in slides %}  
        &lt;li&gt;

            {% set cover = slide.coverImage %}
            {% set coverbackup = slide.entryContent %}

                    {% if cover %}    
                        {% for cover in cover %}
                            &lt;div class="uk-panel-teaser"&gt;
                                &lt;img class="uk-margin-bottom" src="{{ cover.getUrl('thumb700') }}" alt="{{ cover.title }}"&gt;
                            &lt;/div&gt;
                        {% endfor %}


                    {% elseif coverbackup %}
                        {% for block in coverbackup %}
                            {% if block.type == "image" %}
                                {% for image in block.images %}
                                    &lt;div class="uk-panel-teaser"&gt;
                                        &lt;img class="uk-margin-bottom" src="{{ image.getUrl('thumb700') }}" alt="{{ image.title }}"&gt;
                                    &lt;/div&gt;
                                {% endfor %}
                            {% endif %}
                        {% endfor %}
                    {% endif %}

      &lt;/li&gt;
     {% endfor %}

&lt;/ul&gt;
{% endif %}
</code></pre>

<entrymodel>
A problem with {% elseif %} statement
# A problem with {% elseif %} statement

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

Here is my situation. This is not a whole code, only the part, where I'm having a problem. At the back end, model coverbackup is populated with files as well as model cover. If I depopulate cover (remove image from entry), I want to load model coverbackup. I was checking over and over, If I got to my file in matrix field correctly, but It seems I have. 
Than I've tried to check if elseif statement loads anything on cover=false, I got empty output.
{# Get the slides stored in variable "slides" #}
{% set slides = craft.entries.section('handle').type(['article', 'blog', 'news']).order('RAND()').limit(3) %}

    {% if slides %}
    

        {% for slide in slides %}  
        

            {% set cover = slide.coverImage %}
            {% set coverbackup = slide.entryContent %}

                    {% if cover %}    
                        {% for cover in cover %}
                            
                                
                            
                        {% endfor %}


                    {% elseif coverbackup %}
                        {% for block in coverbackup %}
                            {% if block.type == "image" %}
                                {% for image in block.images %}
                                    
                                        
                                    
                                {% endfor %}
                            {% endif %}
                        {% endfor %}
                    {% endif %}

      
     {% endfor %}


{% endif %}

**Tags:** entrymodel


---

## 1 answers

### Answer by username_1 on 2015-03-17. Score: 5 | Accepted answer

The proper conditional statement in this case would be to test for the length property of your Asset field, i.e. cover:
{% if cover | length %}

The reason this works, is that calling slide.coverImage will – as long as the attribute "coverImage" exists for the "slide" model – never return a falsey value, even if there are no files. It will in fact always return an instance of the ElementCriteriaModel class, i.e. an object representing your Asset field.
What the conditional {% if cover | length %} does is to read the length property of the ElementCriteriaModel object. The length property will represent the number of images in the Asset field – if it contains 0 images, the conditional will evaluate to "false" – as the number 0 within a conditional is interpreted as a falsey value. If you prefer, you can also write the statement like this:
{% if cover | length > 0 %}


---

