templates

Put HTML in your code!

Software

Most web developers will tell you to separate your HTML from your code. I'm going to defend an unorthodox view: put HTML in your code!

Let us be honest here: HTML is not going away. Web sites aren't going away. The ones from 1993 still render and most sites haven't even upgraded to the latest standard yet, let alone that they would abandon it alltogether for something radically different. So who cares if there are bits of HTML in your web site application? One could argue that separation would make it easier to expand the engine to radically different presentation layers, but in practice.. who seriously needs that? HTML works pretty well for most people. Add CSS and (now that's useful to normalise and separate, also for the sake of graphic designers), and JavaScript (also easy to separate with includes as dynamic JavaScript code should not be necessary with the right hooks) for xmlHttpRequest and web sites can do very much on many media (screen, print, etcetera).

I also don't think that separate HTML templates make maintenance easier. If the code base is properly normalised, that's sufficient aid to a developer to find what needs editing. HTML templates can contain references to variables, arrays, other templates, Javascript calls and often some simple scripting logic (if/else, loops).and so on. Anyone using them will need to have some skills in working with the code base anyway. Might as well keep it in one language without the proxy template scripting and echo the necessary HTML.

In fact, when a change of logic alters the desired output, it's a lot easier to see bit of HTML within code than both having to look for a template and needing to adjust to the new scripting rules in it.

And just look at the state of affairs: most template systems I've come across tend to lead to extreme amounts of duplication. I have one single line in all of my code which is responsible for, say, the <body> tag. If I've called $page->onLoad() with a string, it will attach onLoad="string()" to the tag and the JavaScript will load. This line is in Page::header() which I need to call on all pages. Most template systems don't even normalise these basics and those who do become a game of constant guessing which string is where.

A method in my User class:

function userPage( $urlOnly = false )
{
$url = "/users/". urlencode( strtolower( $this->login ) ). "/";
if ( $urlOnly )
return $url;
else
return "<a href="$url">". htmlspecialchars( $this->login ). "</a>";
}

Perhaps I could move the "/users/" part to a User::baseURL() variable. I believe there is a wrapper that prepends http:// and the domain name that could be handled in a nicer way. But other than that.. why, for a freaking web site, is this considered to be ugly? I bet my web site code and HTML are far easier to maintain than the majority of template-based systems or other attempts to separate code and output.

Seriously.. my code is full of echo statements, even though a lot of output is first pre-generated in strings by class methods. Definitely beats having a gazillion tiny templates or worse, template scripting in yet another hacked together language.

It's wonderful that some people use Java, PHP or Perl to write a scripting language for their web site, but don't try to tell me it's a silly idea to just write the web site directly.

© Copyright 1995-2009 Robert John Kaper. All rights reserved.

Tom has more friends but mine are prettier! (#1/1)