New macro cycle from smarty

Notice: This thread is very old.
Felix
Nette Core | 1196
+
0
-

I have wondering about new macro {cycle ..}, idea taken from smarty:cycle.

Use case (3 records):

{foreach $rows as $row}
    <a class="label {cycle label-info, label-success, label-error}" n:href="..">{$row->a}</a>
{/foreach}

Output:

<a class="label label-info" href="..">a</a>
<a class="label label-success" href="..">b</a>
<a class="label label-error" href="..">c</a>

Implementation (CoreMacros):

$me->addMacro('cycle', '$_cycle = %node.array; echo $_cycle[$iterator->counter%count($_cycle)];');

What do you think guys? Is it useless or it could helps?

pave.kucera
Member | 122
+
0
-

Do you have a real use case when this would come in handy?

Majkl578
Moderator | 1364
+
0
-

Since PHP 5.5, you can do something like this:

{foreach $rows as $row}
	<a n:class="label, [1 => label-info, label-success, label-error][$iterator->counter]" n:href="..">
		{$row->a}
	</a>
{/foreach}
David Grudl
Nette Core | 8218
+
0
-

Correction:

{foreach $rows as $row}
	<a n:class="label, [label-info, label-success, label-error][$iterator->counter % 3]" n:href="..">
		{$row->a}
	</a>
{/foreach}