Never use .CSS files again
MachII---AKA really really dynamic style sheets---
Recently I blogged about they new way I am serving XML data to my apps and, thinking along those same lines, I decided to serve up the .css “files” in much the same manner. The need to deliver completely separate styles to the same layout is not that uncommon if you are writing an application that will give the user a choice of “skins” (as we do on InstantSpot.com ).
Here is a basic run-down:
Let's say, we have an object “Skin” which has, as one of its attributes, Style. Style contains the actual css info that would normally be housed in your style.css (or whatever you called it) style sheet.
Now, we have a layout that needs to link to some style sheet, or we would have a pretty plain looking site. Normally there would be a link to a file under the web root like:
<link rel="stylesheet" xhref="/skins/style.css" type="text/css" />
Since, in this scenario, we don't really have anymore style.css anywhere we need to have some new way to style our site. One possibility, we could create a Mach-II event called “style.css” that pulls that data from the Skin object, and passes it on to the view.
<event-handler event=”Style.css” access=”public”>
<notify listener=”StyleListener” method=”getStyle” resultArg=”CSS” />
<view-page name=”showCSS” />
</event-handler>
ShowCSS is simple a cfcontent and a cfoutput much like my other dumb XML view.
<cfcontent type=”text/css”>
<cfoutput>#event.getArg(“CSS”)#</cfoutput>
And finally, we can now link in that new style sheet via our Mach-II event.
<link rel="stylesheet" xhref="/index.cfm/event/style.css" type="text/css" />
voila...no more style.css files.
I have used other methods for using dynamic data in a stylesheet before but there is something beautiful to me about using the Mach-II event. Also, the .css extension on the event name might appease the validators out there.
PS. There is more to this story, stay tuned for the real reason we needed “really really dynamic style sheets”





Loading....