Australian Made and Owned

Processing PHP Code in CSS files to Improve Productivity and Browser Cross-Compatibility (using Apache)

It’s possible that you’re thinking, “Processing PHP in CSS? That’s plain stinks.,” just from reading the heading. It does stink, but so does maintaining two CSS files, updating your colour scheme manually (or even by sed), changing common dimensions, and all the other crap that goes with static files. Think about it this way – would you build a site out of many HTML files or would you use some kind of template system (like Pretty Smarties)? By now I hope you’re less sceptical. If you’re interested, please read on.

Executing PHP in CSS Files

In order to parse and execute PHP in a CSS file, all you have to do is tell Apache to treat CSS files as PHP, and just make sure you tell the browser that it’s NOT text/html (default mime type) but CSS.Very easy.

.htaccess File

Place a .htaccess file in the directory of the CSS file (if there isn’t one already). Simply add a directive to process the PHP in CSS files. Depending on your configuration, you might have to use a different directive. Here are the two most common ones I’ve been able to use:
Mod_PHP (Apache PHP Module)

#MOD PHP

	SetHandler application/x-httpd-php

Others:

#OTHERS
AddType application/x-httpd-php5 .css

(If you’re using php4, use x-httpd-php4)

CSS (and PHP) File

I recommend using this code just to test that your Apache directive is working. Hopefully this example shows the potential use for (server-side) executable code in CSS files.

<?php
	//make sure the browser treats this as CSS
	header("Content-Type: text/css");
 
	//browser detection
	if (strpos($_SERVER['HTTP_USER_AGENT'],"Internet Explorer") > 0) {
		$internet_explorer = true;
	}
 
	$baseColour = "#F3AA9C";
	$secondaryColour = "#CCAF1F";
	$foregroundColour = "#FFFFFF";
	$moduleWidth = "125px";
?>
 
#content {
	background:<?php echo $baseColour; ?>;
	colour:<?php echo $foregroundColour ?>;
}
 
#sideBar {
	background: <?php echo $secondaryColour; ?>;
	color:<?php echo $baseColour; ?>;
	width:<?php echo $moduleWidth; ?>;
}
 
#footer {
	color: <?php echo $secondaryColour; ?>;
	background:<?php echo $foregroundColour; ?>;
}

Obviously mixing PHP and CSS stuffs up syntax highlighting sorry.

Caveat

Most browsers will download the CSS file on every pageload – it’s not too difficult to implement cache-control, but I haven’t done it for this code.


Code hard or … er… code home.



Did you like this post?
Email Send to a friend Add to Technorati Favorites

AddThis Social Bookmark Button
AddThis Feed Button

Leave a comment

5 Comments

  1. Mark

    I don’t think you need to serve the PHP with a .css extension. As long as the headers in the php are “text/css” and the file is linked from the HTML correctly (type=”text/css” rel=”stylesheet”) then everything should work ok.

    February 27, 2010 @ 12:17 am

  2. gleapsite

    As a way to improve readability… instead of
    background:;

    Just use

    background:;

    February 27, 2010 @ 4:26 am

  3. gleapsite

    Your blog software ate my php tags. so I’ma try and use parens instead.

    (php $background ?)
    is the same as
    (php echo $background; ?)

    February 27, 2010 @ 4:28 am

  4. Tim

    @Mark, that’s true. It’s not necessary anymore. No modern browser cares about the extension of a stylesheet file.

    March 3, 2010 @ 10:37 am

  5. Tim

    @gleapsite I think you’re talking about short tags and they’re ba-ba-bad. Turn them off and don’t use them! I went through a phase of using them and I also went through a phase of regretting it.

    More: http://stackoverflow.com/questions/200640/are-php-short-tags-acceptable-to-use

    March 3, 2010 @ 10:39 am

RSS feed for comments on this post. TrackBack URL