Take the 2-minute tour ×
Craft CMS Stack Exchange is a question and answer site for administrators, end users, developers and designers for Craft CMS. It's 100% free, no registration required.

I thought this would be very easy but I'm having a hard time figuring it out.

How do you output a warning/message if there are no entries to loop through?

{% set entries = craft.entries.section('news').limit(5).find() %}

{% if entries|length %}
    {% for entry in entries %}
      {# Do Loop #}
    {% endfor %}
{% else %}
    <p>No entries found.</p>
{% endif %}

Or am I just sleepy and approaching this all wrong?

share|improve this question

1 Answer 1

up vote 7 down vote accepted

The for tag has an else clause by which you can render a replacement block if no iteration took place because the sequence was empty.

<ul>
    {% for user in users %}
        <li>{{ user.username|e }}</li>
    {% else %}
        <li><em>no user found</em></li>
    {% endfor %}
</ul>

reference: http://twig.sensiolabs.org/doc/tags/for.html#the-else-clause

share|improve this answer
1  
why did you not check your answer as accepted if it solves your problem? –  dralshehri Jul 12 at 1:08
    
I thought I had, I think my internet connection glitched when I did. Thanks for letting me know! –  cballenar Jul 12 at 1:54
    
Oh, wait... No. I can do it in 14 hours :P –  cballenar Jul 12 at 1:55

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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