Latte foreach – how to check for no values in array

Notice: This thread is very old.
simonjcarr
Member | 28
+
0
-

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 | 2634
+
+1
-
{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)

Aurielle
Member | 1281
+
0
-

The if->foreach is not really needed, but of course depends on your use case. In most of mine templates I can write something like

<div n:foreach="$comments as $comment">...</div>

<div n:if="!count($comments)">...</div>
David Grudl
Nette Core | 8110
+
+4
-

This is possible too:

<div n:foreach="$comments as $comment">...</div>

<div n:if="!$iterations">...</div>
simonjcarr
Member | 28
+
+1
-

Thanks, for all the advice. What a great community Nette has.