Subtraction.com

Individual Style

Someone, somewhere may have come up with this before me — if they did, I never saw it, I swear — but I’m nevertheless proud of this little trick that I developed for version 7.0 of this site. It applies unique styles to individual articles from within Movable Type in a more or less automated way. This post is an example of the trick in action: a little bit of PHP, a little bit of Movable Type hackery and a little bit of CSS all conspire to apply a completely arbitrary variant on the standard, black and white Subtraction.com article. Translation: this page looks different from the others.

One Day Only! Special CSS Styles!

I had long been frustrated with my inability to apply ad hoc style rules to various posts that I’ve made to the site. I’m talking about pieces that have called for one-off treatments to text or graphics — content-specific formatting for informational tables, for instance, or an accommodation for a unique kind of image. These are problems that are easily solved with a new CSS rule, but I never felt comfortable with the idea of ‘polluting’ my basic style sheets with rules that may only get used once or twice.

Then it occurred to me that a little PHP, combined with some Movable Type tags, would solve this problem nicely. Following is a quick explanation of how it works — handily, it doubles as a case for why I’m uniquely under-qualified to ever do serious technical writing.

How It Works

The idea is that the PHP automatically checks for a style sheet with the same name as the permalink of the article — with a .css file extension of course. This style sheet just gets appended to the page’s <meta> header, and adds to or overrides rules included in the default and otherwise active CSS file

The name of this second style sheet is supplied by MT tags in the template which are automatically generated in the published article; I only need to be sure to manually name the style sheet correctly. If PHP finds the matching CSS file, it appends a <link> to the header of the page pointing to that style sheet.

To be honest, I’m no PHP expert, and I had to consult a friend even for this simple construction. He polished it off quickly and gave it to me for free, so I feel like I should ‘pay it forward,’ so to speak. Go nuts — I’m sure some of the more enterprising amongst you are going to come up with applications for this concept that will put me to shame. More power to you.

<?php

// check to see if a css file with the
// same name as the permalink exists,
// if so print a link to it

if (file_exists(".css")) {
     print '<link rel="stylesheet" type="text/css"
     href=".css" />'
   ;}
?>
+