By Douglas Clifton
A frequently asked question on the popular css-discuss.org mailing list and archive is, "How do I store and reuse values in my style sheets?"
I can certainly relate to this as a programmer—it was one of the first things I thought of when I started using CSS. Because I prefer to separate my work into discrete modules, I immediately recognized the power behind the concept. I was already using PHP, Perl and other tools to generate HTML dynamically, so CSS didn’t seem quite powerful enough. However, it didn’t take me long to learn how to approach style sheets in a similar way.
CSS is designed for layout and presentation. Its goal is to restore HTML documents to their original purpose—to be structural in form and to reflect the flow of the content contained in them. In the brave new world of XHTML, this is mandatory.
Want to impress your geek friends? Mention that CSS isn’t even close to being Turing complete. It’s true—CSS has no concept of variables, conditions, loops or many other features inherent in programming languages. The trick is to dynamically generate style sheets using a language that does have these properties. By now, even non-programmers have heard of PHP.
PHP’s penetration into the Web server-side scripting market share is remarkable, and it overtook Microsoft’s ASP three years ago as the top language used by developers. It typically runs on the Apache HTTP server, the most popular server on the Web.
One reason for this explosive growth is that PHP is free and open source. It’s also a relatively easy language to learn and the de facto standard for developers creating dynamic Web pages. But it is also perfectly suited for deploying CSS and many other Web technologies.
To demonstrate this technique I will design a mock up of a basic site layout using CSS. It will include a masthead, menu and content area. I will then assign three color schemes using PHP for the background and foreground colors of each element in the layout and show you how you can easily switch between each one or add new schemes—all from one script. I’ll conclude the article by suggesting ways you can extend or improve on these ideas for your own site.
Dynamic CSS
PHP is an embedded technology, meaning you can combine the logical/programming code with normal HTML or XHTML markup. In a more general sense, the output can be any text you like—in this case, it’s CSS. To toggle between these two modes you simply use special start and end tags:
/ PHP code... /
?>
My Site
/ ... /
?>
You can also send code back to the browser using output functions such as print() or echo(). There are a number of other methods, but I will be using a variation on the print() function known as "heredoc syntax" to return first the persistent style sheet then each alternate.
Before doing anything we must tell the browser what resource type we’re returning. Normally, PHP and the Web server it’s running under will automatically tell the browser that a PHP script is HTML. In the case of a static CSS file, the server knows it’s CSS by the file extension. But this is a special case, so our first job is to send the correct media, or MIME type to the browser using the PHP header() function:
header('Content-type: text/css');
The next step is to define the color scheme sets using associative arrays. An associative array is just a collection of related items, each accessed by a string. Normal arrays use an integer-based index. It’s just another type of variable, and all variables are prefixed by the dollar sign. So, the first of our color schemes, the default or persistent style, is defined like this:
Notice I’m using hexadecimal color values here and not including the pound sign (#) in the color values. We’ll take care of that when we output the CSS code. To reference one color in the array you address the $persistent array using the named or string index. For instance, the value for the masthead background color can be accessed like this:
print $persistent['bgmast']; / output: bbd9ee /
Note: You will see a slight variation on this syntax when we actually use it in the dynamic style sheet. This is due to a conflict between the PHP parser and the CSS code that surrounds it.
If you take a look at the PHP source code in the dynamic style sheet, you’ll see that just as in the example above, I’ve defined two alternate color schemes. Now it’s just a matter of finding a mechanism to select each the three versions.