Tuesday, May 22, 2007

Take your templates and shove 'em...

When I first started programing web pages, I used what was and is generally perceived as bad design... I mixed my HTML and code together. Oh, horror of horrors! I quickly learned that this was bad, even though it was oh so convenient at first: what if you have to change all the HTML? This might happen if you wanted to redesign the look of the site, and it would be much worse if you wanted some non-programmer type to do it.

The solution, of course, was (is) templates. The idea here is to separate your logic (the code) from the presentation (html). Never mind that there is already CSS which is meant to separate your html into presentation (css) and structure (html). HTML and CSS belong to the designers and code to the programmers, right? Hence templates.

So. Templates. At first they were simple. Just do your HTML and put in names surrounded by brackets (ex: {page_title}). The code then loads the template, replaces the bracketed items with data, and spits it out to the browser. Simple enough for the idiot designers to not mess up. A little more anoying for the programmer though.

Then somebody realized that, often, there are parts of the page that get repeated, or are enabled or disabled. How do you do that? Well, you come up with another thing called a block, often this is defined as an HTML comment: followed by . This surrounds the part of HTML you want to repeat, or remove. The code looks for these, removes them, and then lets the code decide to put them back in, possibly multiple times. And the designers were able to understand this concept too, and work around it. The programmers got a little more buggered, but figured it was worth the functionality.

Then some designer (and the moron probably thought he was being really clever) asked why he can't somehow determine how many times a block gets repeated, or whether it showed up at all. So some overly enterprising programmer gave him a way to do it: by implementing blocks that could have a condition or loop construct.

Now wait a minute. Conditions and looping? That's programming talk! Then they started implementing functions. Yes, I'm looking at you, Smarty. What the hell? So now the designers are becoming programmers, and the programmers are just getting really pissed off, but they are told it has to be this way, don't you remember how it used to be?

Yeah, I do. I remember when I didn't need an extra layer of crap that forces me to separate things when I could have done it myself, and slows things down. Granted this makes sense if you are doing web pages in C or Java or something, but in PHP?

PHP was originaly designed to do what template systems do today.. make it easy to embed code and HTML. PHP *is* a template language (and you can do the same thing with ASP and other languages). And now we implement another template language inside of it? DUH?

So, lets summarize the differences:

Templates: {variabletobereplaced}
PHP: <?php echo $variable ?>
or using short tags: <?=$variable ?>

Is that really to much harder? And its SO much easier in the code, no loading a template, no setting the variables with some function call. Just assign the variable, and done. If you want to keep your "template" in a separate file, just include it.