Related link: http://www.oreillynet.com/pub/wlg/4863

In my previous post called Getting PHP to make the HTML for me - I mistakenly called what I was doing a template system. But I don’t want a template system. I want shortcuts, inside PHP, that output the HTML I need.

All good geeks know that repetition of information is usually a problem waiting to be solved. HTML (even in templates) is filled with SO much repetition I can’t help but try to optimize my use of it.

But first - here’s a couple examples why I don’t want to use templates for my web-making:

One template extreme: mostly static HTML with some variables:

<html><head><title>{$title}<title></head><body>
<table border="0" cellpadding="5" cellspacing="0">
<tr><td class="bgcolor1">
<h1 class="header">{$title}</h1>
</td></tr>
<tr><td class="navbar">
{$navigation_here}
</td><td>
<h2 class="title">{$article_title}</h2>
<p>{$article}</p>
</td></tr>
<tr><td>
&copy; {$year} {$author}
</td></tr></table>
</body></html>

My problem with mostly-static templates:

Why not just use PHP? In that sense PHP *is* a templating language. Why wrap a new language around something that PHP already does just fine? Better explained in Brian Lozier’s article on Template Engines. A great read.



Another template extreme: mostly display-logic and variables with some HTML inside:

{include file="pagetop.tpl"}
{if isset($invalid)}
Invalid edit option ({$edit})
{else}
{foreach key=fieldname from=$fields item=element}
{if $expert or !$element.expertonly}
<tr><td width="40%" align="right" valign="top">
<b>{$element.description}</b>
{if $element.subdesc}
<br /><span class="tiny">{$element.subdesc}</span>
{/if}
{strip}
</td><td width="60%" align="left" valign="middle" class="small"
{if $element.crucial} bgcolor="Yellow"{/if}>
{/strip}
{if isset($notype)}
<p>Not ready to upload yet</p>
{else}
{assign var="file" value=$element.type|concat:".tpl"}
{include file="Backend/Bits/$file"}
{/if}
</td></tr>
{/if}
{/foreach}
{/if}
{include file="pagebottom.tpl"}

My problem with mostly display-logic and variables templates:

Again - why not just use PHP? Look at that code example, above. 26 lines of code, and only 6 lines are actually HTML. Why even be in a template, then? Why not just stay in PHP and generate those occasional tidbits of HTML when you need them?

I disagree with the philosophy that tries to keep the "poor non-technical graphic designers" away from PHP, giving them an "easier" system like Smarty or any other template language. Again: why not let them use PHP for their template-logic? It’s much more documented and well-known than any special template-language you can throw at someone.

I make very interactive websites. Full of display-logic that will (for example:)

  • display different flags if an item is on sale
  • use different image sizes based on how many images are shown at once
  • totally change the look and feel if clicking from a partner site
  • show different languages and currency based on your settings

As you can imagine, using template files for this kind of on-the-fly display logic was getting ridiculous.

I decided I don’t want to leave PHP anymore to output the occasional HTML tags around my variables.

That’s where my head was at when I wrote the last post, "Getting PHP to make the HTML for me".

Read that, if you haven’t, then let’s move on to the next idea: basic HTML building blocks.

Your thoughts on this so far? Do you use and love templates for interactive sites? Am I missing the point, here?