There are plenty of reasons why we sometimes need to have the first or last element in a row to have a different Class than its siblings. If, for example, I wanted to create an unordered list of which the last list item was to contain a different CSS rule than any of the other list items, I'd simply create a class for it within my CSS stylesheet and reference that via the class attribute inside my HTML element:
<ul id="nav"><li><a href="homepage.html">Home</a></li><li><a href="about.html">About</a></li><li class="last"> <a href="contact.html">Contact</a></li></ul>
I might also want to change the last class attribute for the third weblog entry because I've decided that I want my last column to not have any margins.
There are plenty of ways to go about altering the first, last, or odd item, but I've chosen to use one of the very simple means, namely using ExpressionEngine's built-in conditional global variables.
So, to be clear: What I want is to have three columns in a row. All three columns have the same class, in this case the class is column. I want the third column to have the additional CSS class last-item because I'd like to change the value of this item's margin. Yes, we could use CSS' ":last-child" or "nth-child" property, but that's for another day. Today, I'd just like to show you how you can manipulate small chunks of code - or large chunks for that matter - by simply using EE's conditionals and variables. You can apply this logic to pretty much any piece of code you'd like to modify.
Raw HTML code without ExpressionEngine tags:
<div class="column"><h2>My title</h2><p>My excerpt</p></div>
But we want to take advantage of EE's tags and create dynamic content, so our code will now look like this:
{exp:weblog:entries weblog="blog" limit="3"}<div class="column"><h2>{title}</h2><p>{excerpt}</p></div>{/exp:weblog:entries}
The third entry needs a second class appended to the HTML. I'm giving it a class name of "last-item".
{exp:weblog:entries weblog="blog" limit="3"}<div class="column {_if count == 3}last-item{/_if}"><h2>{title}</h2><p>{excerpt}</p></div>{/exp:weblog:entries}
(remove the underscore before the opening and closing if)
What's changed? If you look at the class attribute of the div, you'll see that I've added a conditional global variable, namely the if conditional. In addition to that I've applied the single variable {count}. In plain English it reads as follows: "If the item is equal to 3 (the third item), then append the CSS class last-item to the HTML element."
And that's it. There are plenty of conditional global variables and single variables that allow for tons of flexibility without resorting to add-ons.
In closing it should be noted that this technique works for EE2 as well. Replace weblog with channel and you're good to go.


Ralph Mason writes on Tue Aug 3, 2010
Nice post, Maleika. I haven’t tried that before (still learning a lot about EE). The way I have been doing this is like so:
<div class=”{switch='column|column|column-last'}”>
I love {switch}, but perhaps it’s less efficient in this instance. (It’s better if you have multiple rows of items, such as gallery thumbnails.
Maleika Esther Attawel writes on Tue Aug 3, 2010
Yes, good point. The switch function can be used as well. I haven’t tested whether there are performance benefits in using either techniques. Might be good to try it out and see which one is more efficient in terms of resources.
Oh, and sorry that you have my logo in your comment. One of those bugs I still need to get fixed.