Latte foreach – how to check for no values in array
Notice: This thread is very old.
- simonjcarr
- Member | 28
Is there a method in Latte that lets you check if an array has any values before running a foreach? I was thing of something like
{foreach $comments as $comment}
{$comment->comment}
{foreach_else}
Sorry there are no comments
{/foreach}
- Šaman
- Member | 2659
{if count($comments) > 0} <!-- dirty: count($comments) -->
{foreach …} {/foreach}
{elseif}
Sorry …
{/if}
or
<ul n:if="count($comments) > 0"> <!-- dirty: count($comments) -->
<li n:foreach="$comments as $comment">{$comment->text}</li>
</ul>
<div n:if="count($comments) === 0"> <!-- dirty: !count($comments) -->
Sorry …
</div>
Last edited by Šaman (2015-10-25 10:27)
- David Grudl
- Nette Core | 8218
This is possible too:
<div n:foreach="$comments as $comment">...</div>
<div n:if="!$iterations">...</div>