Before you read this, if you are a bona fide CSS guru, don't laugh at me. Remember I'm just a Coldfusion code jockey with only a bare minimum of aesthetic sensibility and I see CSS with the same approximate mystery as I see women's obsession with shoe shopping. That being said, this is a neat trick for alternating row colors. You know what I mean - you have this long query that is going to be displayed in tabular HTML. You'd like to alternate each row to make the data stand out and be easier to read.
Now in lots of my old code there are CFIF statements like this
<td bgcolor="<cfif currentrow mod 2>##FFFFF<cfelse>##CCCCC</cfif>">#columname#</td>
Ooh! Just looking at it probably gives you a shudder. Looking at more recent code you might see something like this:
<td style="background-color: <cfif currentrow mod 2>##FFFFF;<cfelse>##CCCCC;</cfif>">#columname#</td>
Well, it's better, but it's still ugly.
Now very recent code might have 2 classes in the CSS file like this:
<style type="text/css">
td.results {
padding-top: 2px;
padding-bottom: 2px;
border-right: 1px solid #000000;
padding-left: 3px;
padding-right: 3px;
background-color: #E0DFCB;
}
td.results2 {
padding-top: 2px;
padding-bottom: 2px;
border-right: 1px solid #000000;
padding-left: 3px;
padding-right: 3px;
background-color: #FFFFF;
}
</style>
Followed by the following type Coldfusion code:
<td class="<cfif currentrow mod 2>results<cfelse>results2</cfif>">#columname#</td>
Ok, now we are getting somewhere. I can style a number of elements besides just the background color using the class - nice! The only thing that bugs me is that "cfif" - it's so ugly. How about leveraging that little extra "feature" that comes with CFMX that allows you to encapsulate the output of an expression that results in a primitive variable. Uh... that means you can put expressions that have output right in between pound signs and the output of the expression is sent to the output buffer. For example, if you did #currrentrow MOD 2# "in-line" it would result in a 0 or a 1 being sent to the output buffer. We can use that little trick to our advantage with some creative class names like this:
<style type="text/css">
td.results0 {
padding-top: 2px;
padding-bottom: 2px;
border-right: 1px solid #000000;
padding-left: 3px;
padding-right: 3px;
background-color: #E0DFCB;
}
td.results1 {
padding-top: 2px;
padding-bottom: 2px;
border-right: 1px solid #000000;
padding-left: 3px;
padding-right: 3px;
background-color: #FFFFF;
}
</style>
Then we can simply output "results#currentrow mod 2#" in the class attribute - as in this example:
<td class="results#currentrow mod 2#">#name#</td>
<td class="results#currentrow mod 2#">#street#</td>
<td class="results#currentrow mod 2#">#city#</td>
<td class="results#currentrow mod 2#">#state#</td>
<td class="results#currentrow mod 2#">#Zip#</td>
This results in HTML output that has class="results0" followed by class="results1" for every other row. Now, some CSS guru is going to light me up and tell me how this could be done much easier using JUST CSS - no Coldfusion at all. Bring it on!