A Method for CSS Variables
Jay Salvat at nettuts has written an article on adding variables to your css files. I like the approach, which includes caching the results, though the code presented is not 100% secure, as commenter Pelle has noted.
As for the most popular response, having apache run css files as php code, there are a few issues that come to mind that the article’s approach takes care of: caching, cleanliness, and portability.
Caching, as the author notes, reduces the number of CPU cycles the server must devote to php processing of the css files. The parser also implements HTTP features for determining if the file has been modified and needs reloading. Simply setting the server to parse CSS files, you may or may not lose these features, depending on your server settings.
The method is also clean; using a parsing class lets you put simple variable assignments directly in the CSS file. Associating CSS with the PHP parser requires you to use the PHP tags and all the appropriate PHP markers ( <?php $var = “whatever”; ?> ); if you miss a semicolon, a quote, a dollar sign, or any other part of the required “PHP syntax”, your css file at best will contain a php error or warning message, making the code invalid, or at worst will not display at all, giving a blank css file. The parser Jay gives, while requiring a syntax of its own, is much simpler to use.
Jay’s solution is portable; the only server configuration necessary is enabling apache’s mod_rewrite module, which should be enabled anyway. If that is done, you can move the .htaccess and enhanced_css.php files to any server, and it will work without further server config.
As commenter Pelle notes, however, if someone knows you’re using this method and they know the path to your enhanced_css.php file, they could use it maliciously. Jay responds with a quick fix that makes sure the class only parses files ending with “css”, but I believe that further security could be provided, though I’ve not taken the time to analyze the code enough to make suggestions.
Chris Pratt made a suggestion of changing the parser to hunt out CSS variables as proposed by Daniel Glazman and David Hyatt. I thought this would be a good idea; the syntax proposed in that doument is much more like the CSS3 stuff we’re slowly getting used to, such as :not() and attr().
For those interested, you could read why some think CSS variables are harmful.
Subscribe via Email Alerts
Subscribe in an RSS reader
What is RSS?
Eternicode on Twitter
Leave a Comment