Alternator Viewtool

Last Updated: Jun 17, 2024
documentation for the dotCMS Content Management System

An Alternator is an iterator to alternate between a couple values or even a list, if it reaches the end of the list, it will start over again at zero.

Methods

MethodReturn ValueDescription
make(List list)AlternatorReturns an iterator that alternates through the list passed in.
make(boolean auto, List list)AlternatorReturns an iterator that alternates through the list passed in. The auto boolean determines if it alternates automatically.
make(Object[] array)AlternatorReturns an iterator that alternates through the array of objects passed in.
make(boolean auto, Object[] array)AlternatorReturns an iterator that alternates through the array of objects passed in. The auto boolean determines if it alternates automatically.
make(Object o1, Object o2)AlternatorReturns an iterator that alternates between the objects passed in.
make(boolean auto, Object o1, Object o2)AlternatorReturns an iterator that alternates between the objects passed in. The auto boolean determines if it alternates automatically.

Examples

Example 1: This creates an alternator for switching between red and blue.

    #set( $color = $alternator.make('red', 'blue') )

Example 2: This creates an alternator to iterate through the list (hip, fly, groovy)

    #set( $style = $alternator.make(false, ['hip','fly','groovy']) )

Example 3: You can send as a parameter, an array or a collection, in addition can set a configuration property called “auto”, if it is true anytime the iterator is referrer will print the current value and move to the next one, for instance:

    #set( $numbers = $alternator.make(true, '1', '2') )
    $numbers <br/>
    $numbers <br/>
    $numbers <br/>
    $numbers <br/>
    $numbers <br/>

The output will be

    1
    2
    1
    2
    1

Example 4: Now if the “auto” is on false, such as

    #set( $numbers = $alternator.make(false, '1', '2') )

The output will be

    1
    1
    1
    1
    1

Example 5: In order to alternate between values you have to call next, such as

    #set( $numbers = $alternator.make(false, '1', '2') )
    $numbers.next <br/>
    $numbers.next <br/>
    $numbers.next <br/>
    $numbers.next <br/>
    $numbers.next <br/>

Toolbox.xml Configuration

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>

On this page

×

We Dig Feedback

Selected excerpt:

×