-
-
Save jCrip/f418aad8579a4a010cbd to your computer and use it in GitHub Desktop.
Cycle function like the one in liquid or twig
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php for( $i = 0; $i < 9; $i++ ) : ?> | |
<p><?php echo cycle('one', 'two', 'three') ?></p> | |
<?php endfor; ?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php for( $i = 0; $i < 9; $i++ ) : ?> | |
<p class="<?php echo cycle('odd', 'even') ?>"> | |
<?php echo cycle('one', 'two', 'three', ':counter') ?> | |
</p> | |
<?php endfor; ?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Cycles through each argument added | |
* Based on Rails `cycle` method | |
* | |
* if last argument begins with ":" | |
* then it will change the namespace | |
* (allows multiple cycle calls within a loop) | |
* | |
* @param mixed $values infinite amount can be added | |
* @return mixed | |
* @author Baylor Rae' | |
*/ | |
function cycle($first_value, $values = '*') { | |
// keeps up with all counters | |
static $count = array(); | |
// get all arguments passed | |
$values = func_get_args(); | |
// set the default name to use | |
$name = 'default'; | |
// check if last items begins with ":" | |
$last_item = end($values); | |
if( substr($last_item, 0, 1) === ':' ) { | |
// change the name to the new one | |
$name = substr($last_item, 1); | |
// remove the last item from the array | |
array_pop($values); | |
} | |
// make sure counter starts at zero for the name | |
if( !isset($count[$name]) ) | |
$count[$name] = 0; | |
// get the current item for its index | |
$index = $count[$name] % count($values); | |
// update the count and return the value | |
$count[$name]++; | |
return $values[$index]; | |
} | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$things = array('shoe', 'sox', 'sand', 'laptop', 'guitar', 'bag', 'legos', 'elephpant', 'stapler', 'binder', 'desk', 'chair'); | |
foreach( $things as $thing ) : | |
?> | |
<div class="grid_4 <?php echo cycle('alpha', '', 'omega') ?>"> | |
<?php echo $thing ?> | |
</div> | |
<?php if( cycle(false, false, true, ':use_clear_div') ) : ?> | |
<div class="clear">// clear div every three items</div> | |
<?php endif; ?> | |
<?php endforeach; ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment