Alternator Viewtool documentation for the dotCMS Content Management System

Utility class for easily alternating over values in a list.

Mapping in Toolbox.xml: Alternator

The following example shows how the Alternator Viewtool is mapped in the toolbox.xml file:

    <tool>
       <key>alternator</key>
       <scope>application</scope>
       <class>org.apache.velocity.tools.generic.AlternatorTool</class>
    </tool>

Example Usage:

 java...
      String[] myColors = new String[]{"red", "blue"};
      context.put("color", new Alternator(myColors));
      String[] myStyles = new String[]{"hip", "fly", "groovy"};
      // demonstrate manual alternation with this one
      context.put("style", new Alternator(false, myStyles));

 template...
      #foreach( $foo in [1..5] )
       $foo is $color and $style.next
      #end

 output...
      1 is red and hip
      2 is blue and fly 
      3 is red and groovy
      4 is blue and hip 
      5 is red and fly

For more information, please review our javadoc on the Alternator Viewtool