This is old content! The graveyard is a snapshot of content created from 2002-2005. For new stuff, visit Maniacal Rage.
 

How This Site Works, Part II

posted on august 2, 2004, tag: site

One of the great things about CSS is the ability it gives you to separate style from content. By simply changing a single stylesheet you can drastically alter the look and feel of a site without ever having to change any content. To strengthen the separation, one should avoid placing markup in their content as much as possible, only including absolutely necessary tags (like b, blockquote and a href amoung others). By doing this, you make the task of reusing your content much easier and cleaner. The less markup you have in your content, the easier it is to completely change your site by changing only the stylesheet.

But what if you need to reuse your content in a format other than XHTML? What if, on a whim, I wanted to create a mobile version of this site in WML or I wanted to browse my content in an OS X application? In these cases, the separation of style from content does nothing. At least not if your content is in XHTML format. Why should the content of this site—which is not specific to the Internet—be stuck in a format only recognized by the web browsers? It shouldn't. How can we take this separation a step further? How can we separate style from format from content? We use XML.

What in the hell is XML?

To quote the W3C, XML is "a simple, very flexible text format derived from SGML." What does that mean to you? It means that XML is a language for creating structured documents. Your documents can be extremely simple or extremely complex, it doesn't matter. XML is data and markup only, nothing else. No style, no look, just content and markup. There is no specific tag set, so your documents can be customized to truly describe your data and fit your needs.

To say that XML is portable is not quite enough—XML is portable and reusable. Because it's only structure and data, you can have one XML file that you reuse for countless purposes—be it XHTML for browser display, WML for mobile display, or in its pure form for use in a Cocoa application. One XML file, limitless use.

But how can a file composed of only textual data and markup be used for all these different purposes? You transform them using XSL. Using XSL, you can format your XML data how ever you wish and display it in any format desired. It's really quite powerful.

So how does this site work already?

Maniacal Rage is a fairly simple collection of similar data. I have a weblog comprised of "entries" and "comments." The entries can be assigned to "categories," and comments can only be assigned to entries. Other data on the site exists in a much looser fashion; the about section contains data related to site/author details, the photos section contains lists of photo collections and so on. All of my data is stored in MySQL.

When you request a page, a simplified way of describing the process is to say that I pull data out of the DB, build a valid XML file and transform it using XSL into a valid XHTML file which is styled using CSS. MySQL » PHP » XML » XSL » XHTML+CSS. Yes, I know I said simplified. I'm sure a 5 step process doesn't sound simplified, though. And while it's true that it would be faster to go directly from MySQL to PHP to you, there is a reason for the extra steps.

See, if you add the RSS subdomain (e.g., rss.maniacalrage.net), I don't give you XHTML+CSS. Instead, I transform the XML using a different XSL stylesheet into valid RSS. Same XML file, same content, different XSL file, different output format. Same goes for Atom and anything else I can think up. Or you can simply view the raw XML for any page by adding the XML subdomain (e.g., xml.maniacalrage.net).

Perhaps you're a visual person, though, and you'd like to see a simple diagram for how this site functions, from request to output. Well, I've got you covered:

From request to output, the Maniacal Rage data model

You'll note that .htaccess is the first link in the chain. This is an extremely important part of how this site works. Other than the photos section and other places where using actual directories is necessary (like where I store images and stylesheets), none of the directories you see in my URLs actually exist. When you visit /archives/, you're actually visiting /index.php?section=archives. I use .htaccess to take the URL you request and properly send queries to a PHP file (and there's only one of them). That file handles retrieving content for the section you've requested. I use mod_rewrite for all of my URLs.

In the next installment of this series, I'll get down to the nitty-gritty and give you some code snippets and such so that you might have a better hands-on understanding of just how things work under the hood.

This system allows for completely reusable content. Hell, you could have your own XSL file (if I were to let you) and make Maniacal Rage look however you want. Or I could use an XSL file to dump the contents of the site into the form of a book. The possibilities are endless. As always, I welcome your questions and comments.

This entry is part II in a series focusing on how this site works. Stay tuned for more.

Comments

There are 13 comments, comments are closed

compuwhiz7 on 08/02/2004:

Yay! :)

So if I understand things correctly, /archives/ does not exist physically, but is instead a dynamically processed and generated "virtual" file? Very neat. Now I know what to do in ASP.NET.

Oh, and pretty graph, too!

Thanks for the information... I can't wait for part III, if there is to be one.

Rob G on 08/02/2004:

Yes compuwhiz, /archives/ doesn't exist. Using modrewrite he is able to transform his URLs into a "clean" look rather than the traditional way. modrewrite uses regular expressions also. You can set mod_rewrite up so instead of http://website.com/index.php?section=about&subsection=author, you can simply visit http://wesite.com/about/author/ instead. Very useful :)

Thanks for doing Part II to this site. I've been wondering when it would come out :) I can't wait for Part III, hope it's not too long of a weight.

By the way, could you tell me the font that's used on the "maniacal rage" header and the diagram? It looks very nice.

Erik Price on 08/02/2004:

XSL — ! {splorch!} That's the sound of my vomit hitting the monitor.

Clearly a lot of hard work has been put into making this site look and run great, and it shows. But I pity anyone who subjects themself to XSL, unless they're using some kind of tool to do most of the dirty work. Such an ungainly technology.

Andy S on 08/03/2004:

Just as a side note - what app did you use to create your functionality diagram or did you draw it in illustrator or something? I have been looking for a nice package for this for ages. Omnigraffle is good but I wondered if you use something else?

Andreas on 08/03/2004:

It looks like it was made in Omnigraffle ver3 I think?

Garrett on 08/03/2004:

Andy—yes, Andreas is correct. OmniGraffle 3.

Rob G—header font is Myriad, font in the diagram is Lucida Grande.

Also, part three will have code snippets, so you'll actually get to see how I'm using .htaccess and XML/XSL.

Scott on 08/03/2004:

Is it possible to redirect someone ONLY if they are from a certain referer? (but not with mod_rewrite, because that hides the real URL)

matthew welty on 08/04/2004:

simply awesome. i'm learning more here everyday.

Steven Burns on 08/05/2004:

Don't take this the wrong way, but I LOVE YOU for taking the time to write these things out and for being an excellent writer and teacher.

Simon Ramov on 08/08/2004:

Garrett, thanks for sharing with us.

For a damn long time I want to build a site using this process, but there is big lack of tutorials on this.

Now, it would be a dream come true if you — or someone else, put up a nice zip file with a basic, but fully functional system like yours — and make it free under some Creative Commons licence... :)

Sime on 08/09/2004:

Maybe you can make your CMS/blogging system free, make a seperate site for it and accept donations? Or at least make it public if not free, a resonable one-time price would be ok.

I think it would be huge hit, wordpress and several other would then be a thing of past... :)

Michael Simmons is working on its own, called TypeWorks (has exellent logo by the way):

http://www.thoughtanomalies.com/archives/2004/08/07/thingsofthisandthat_nature/

Jeff on 08/13/2004:

I'll chime in here on the theme of open sourcing your cms, or at the the mysql->xml->(xslt)->xhtml part ;)

There really is a paucity of tutorials on using xslt to generate valid xhtml and it's something the web standards crowd should really get into.

Meanwhile, thanks for posts like this.

Jeff on 08/13/2004:

Hmm... yer comments engine slashed up my post because of some unencoded right angle brackets.

For the record, the first paragraph was just another call to open source the cms for this site (or at least the sql-xml-xslt-xhtml part ;)


See Also

View the archive

Original iPod Introduction
How far we've come in just a few short years. Here's where it all started.

Front Row on Non-iMacs
Going to try this tonight!

WriAShorStorWe!
DY starts a one-week short story writing event for people to lazy to enty NaNoWriMo. VerCooIdea.

Lost Rhapsody
Funny Flash movie using Weird Al music and Lost stuff. Lyrics make a surprising amount of sense!

Jed's Other Poem
Unsolicited music video made on an Apple ][. Fantastic!

Printers Output Secret Barcode
The government is keeping tabs on what you print, with the help of major printer companies.

Dreamhost Promo Codes
DH already has very cheap, very good hosting—this just sweetens the deal.

Photos of the new iPod
Just received my new iPod and I put a few photos up.

PEZ MP3 Player
Funny idea that actually looks kind of neat. I like that it comes pre-loaded with "indie" music.

HD Easter Egg
"My Name is Earl" on NBC gives viewers with HD TVs a little easter egg. Cute, but weird.