Alternating Values in MT Templates
When presenting a running list of items like comments or posts in an archive, designers will provide visuals cues to indicate where one item ends and the next begins. One of those methods that is commonly used is the subtle alternating background colors. Here is a screenshot of the technique in use:

MT doesn't have a straight forward means of alternating or determining if the count is an odd or even number. The Gizmos for MT collection has a variable tag that makes that easy:
<$mt:alternator$>
Each time that template engine passes over the mt:alternator tag it outputs either "odd" or "even" by default. Using these labels as CSS class names we can alternate the style of each item:
.odd { background-color: white; }
.even { background-color: silver; }
We then apply the mt:alternator tag to the markup:
<mt:comments>
<div class="<$mt:alternator$>">
...
</div>
</mt:comments>
This will produce HTML something like:
<div class="odd">
...
</div>
<div class="even">
...
</div>
<div class="odd">
...
</div>
...and so on.
Perhaps "odd" and "even" are too drab for you or you're more of a inline styles designer. We pass no judgement. Use the odd and even attributes to set different values or the tag to alterate between:
<mt:comments>
<div style="background-color: <$mt:alternator odd="white" even="silver"$>;">
...
</div>
</mt:comments>
Instead of "odd" and "even" the tag alternates between "white" and "silver."
The mt:alternator tag can track multiple alternators in one page by using the optional name attribute. For instance, you want to list comments and pings on one entry page and use the alternator effect.
<mt:comments>
<div class="comment-<$mt:alternator name="comments"$>">
...
</div>
</mt:comments>
<mt:pings>
<div class="ping-<$mt:alternator name="pings"$>">
...
</div>
</mt:pings>
Regardless of whether the list of comments ended with an "odd" or "even" value, the pings list will always begin with "odd."
For more details on this tag and more see the Gizmos for MT documentation in the knowledge base.

2 Comments