
Put HTML in your code!
- Posted by Rob (#1) on August 1, 2006 23:48 CEST
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.
- Tags: PHP, web sites, HTML, code seperation, normalisation, templates
Comments
Rob (#1) on Aug 1, 2006 23:59 CEST (Post reply)
I assume active maintenance, development or research. If whatever you have just gathers virtual dust, there is indeed no point in discussing code design.
Neil Stevens (#3) on Aug 2, 2006 00:02 CEST (Post reply)
Very well, but in that case, it's bad design to spread out your html throughout the the code. Any significant html changes will be invasive. That's not what good modular code should be like.
Rob (#1) on Aug 2, 2006 00:13 CEST (Post reply)
Ah, but when you use templates, your logic invades your HTML and that's far worse. Code logic and available data (new tables and columns) evolve far more rapidly than HTML.
Sure, when the anchor tag is changed expanded, my User::url() method is in trouble. Not going to happen soon. But with scripted templates, one would often need to change the so-called separated HTML template for the sake of logic!
Neil Stevens (#3) on Aug 2, 2006 00:15 CEST (Post reply)
Don't we WANT display logic to be with display html, and separated from other logic, such as from database access?
Rob (#1) on Aug 2, 2006 00:23 CEST (Post reply)
Oh definitely, I agree, separating display logic from application logic is good. I just don't like how a lot of people seem to think this separation requires templates or abandoning the language they're already writing in.
Neil Stevens (#3) on Aug 1, 2006 23:50 CEST (Post reply)
Seems to me your logic in the opening only works if you do intend to just toss out any old html that may or may not render today OK.
First javascript, now this... You're slipping!