New macro cycle from smarty

5 years ago

Felix
Nette Core | 889
+
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?

5 years ago

pave.kucera
Member | 122
+
0
-

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

5 years ago

Majkl578
Moderator | 1379
+
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}

5 years ago

David Grudl
Nette Core | 6790
+
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}