GWcode Alternate

http://gwcode.com/assets/img/add-ons/tn/gwcode-alternate.gif

This simple plugin for ExpressionEngine 1 and 2 lets you rotate through any number of values to create alternations, for CSS classes for example.

I've created this because I needed to alternate colors for categories while the switch variable isn't available for the exp:channel:categories tag.

Searching the ExpressionEngine forums, I've noticed I'm not the only one needing this. Some threads discuss the use of PHP (which is fine, but I'd rather not allow PHP in my templates), while others mention a plugin called "SwitchPlus" (which I couldn't find and may not have an EE 2 version):

So, here's how you would use the GWcode Alternate plugin:

Parameters

ParameterPossible Value(s)ExampleDescription
counta number, or variable that contains a numbercount="{count}"The number that should be matched against the values in the alternate parameter.
alternateone or more values, seperated by a pipe characteralternate="one|two|three"The plugin will rotate through these.

Variables

VariableDescription
{alternate_value}The value from the alternate parameter that should be returned if you use a tag pair (see example 2 below).

Example 1

Using a single tag:

<ul>
	{exp:channel:categories channel="example" style="linear"}
		<li class="{exp:gwcode_alternate count="{count}" alternate="uneven|even"}">{category_name}</li>
	{/exp:channel:categories}
</ul>

..will output:

<ul>
	<li class="uneven">Category 1</li>
	<li class="even">Category 2</li>
	<li class="uneven">Category 3</li>
</ul>

Example 2

Using a tag pair:

<ul>
	{exp:channel:categories channel="example" style="linear"}
		{exp:gwcode_alternate count="{count}" alternate="one|two|three"}
			<li class="{alternate_value}">{alternate_value}: {category_name}</li>
		{/exp:gwcode_alternate}
	{/exp:channel:categories}
</ul>

..will output:

<ul>
	<li class="one">one: Category 1</li>
	<li class="two">two: Category 2</li>
	<li class="three">three: Category 3</li>
</ul>