1- # View Helper - Cycle
1+ # Cycle
22
33The ` Cycle ` helper is used to alternate a set of values.
44
@@ -9,7 +9,7 @@ To add elements to cycle, specify them in constructor:
99``` php
1010<table >
1111 <?php foreach ($this->books as $book): ?>
12- <tr style = " background-color : <?= $this->cycle([' #F0F0F0 ' , ' #FFF ' ])- >next () ?>" >
12+ <tr class = " <?= $this->cycle(['odd ', 'even '])->next() ?>" >
1313 <td ><?= $this->escapeHtml($book['author']) ?></td >
1414 </tr >
1515 <?php endforeach ?>
@@ -20,10 +20,10 @@ The output:
2020
2121``` php
2222<table >
23- <tr style = " background-color : #F0F0F0 " >
23+ <tr class = " odd " >
2424 <td >First</td >
2525 </tr >
26- <tr style = " background-color : #FFF " >
26+ <tr class = " even " >
2727 <td >Second</td >
2828 </tr >
2929</table >
@@ -32,15 +32,15 @@ The output:
3232Instead of passing the data at invocation, you can assign it ahead of time:
3333
3434``` php
35- <?php $this->cycle()->assign(['#F0F0F0 ', '#FFF']) ); ?>
35+ <?php $this->cycle()->assign(['odd ', 'even'] ); ?>
3636```
3737
3838You can also cycle in reverse, using the ` prev() ` method instead of ` next() ` :
3939
4040``` php
4141<table >
4242 <?php foreach ($this->books as $book): ?>
43- <tr style = " background-color : <?php echo $this->cycle()- >prev () ?>" >
43+ <tr class = " <?= $this->cycle()->prev() ?>" >
4444 <td ><?php echo $this->escapeHtml($book['author']) ?></td >
4545 </tr >
4646 <?php endforeach ?>
@@ -51,11 +51,11 @@ The output of the two previous examples combined becomes:
5151
5252``` php
5353<table >
54- <tr style = " background-color : #FFF " >
55- <td >First</td >
54+ <tr class = " even " >
55+ <td >First</td >
5656 </tr >
57- <tr style = " background-color : #F0F0F0 " >
58- <td >Second</td >
57+ <tr class = " odd " >
58+ <td >Second</td >
5959 </tr >
6060</table >
6161```
@@ -64,12 +64,12 @@ The output of the two previous examples combined becomes:
6464
6565If you are nesting cycles, you must provide all but one of them with a name; do
6666this by providing a second parameter to the ` cycle() ` invocation:
67- ` $this->cycle(array('#F0F0F0 ', '#FFF '), 'cycle2') `
67+ ` $this->cycle(array('odd ', 'even '), 'cycle2') `
6868
6969``` php
7070<table >
7171 <?php foreach ($this->books as $book): ?>
72- <tr style = " background-color : <?= $this->cycle([' #F0F0F0 ' , ' #FFF ' ])- >next () ?>" >
72+ <tr class = " <?= $this->cycle(['odd ', 'even '])->next() ?>" >
7373 <td ><?= $this->cycle([1, 2, 3], 'number')->next() ?></td >
7474 <td ><?= $this->escapeHtml($book['author']) ?></td >
7575 </tr >
@@ -89,12 +89,12 @@ As a combined example:
8989
9090``` php
9191<?php
92- $this->cycle()->assign(['#F0F0F0 ', '#FFF '], 'colors ');
92+ $this->cycle()->assign(['odd ', 'even '], 'classes ');
9393$this->cycle()->assign([1, 2, 3], 'numbers');
9494?>
9595<table >
9696 <?php foreach ($this->books as $book): ?>
97- <tr style = " background-color : <?= $this->cycle()- >setName ('colors ')- >next () ?>" >
97+ <tr class = " <?= $this->cycle()->setName('classes ')->next() ?>" >
9898 <td ><?= $this->cycle()->setName('numbers')->next() ?></td >
9999 <td ><?= $this->escapeHtml($book['author']) ?></td >
100100 </tr >
0 commit comments