Article

Home » Server-side Coding » PHP & MySQL Tutorials » Beyond The Template Engine

About the Author

Brian Lozier

author_brian Brian is a contract Web developer currently working at RealImpact, a division of Real Networks that provides content management solutions to non-profit organizations.

View all articles by Brian Lozier...

Beyond The Template Engine

By Brian Lozier

September 18th, 2003

Reader Rating: 8.5

Page: 1 2 3 4 5 Next

In general, template engines are a "good thing."

I say this as a long time PHP/Perl programmer, user of many template engines (fastTemplate, Smarty, Perl's HTML::Template), and as author of my own, bTemplate.

However, after some long discussions with a co-worker, I've decided that the vast majority of template engines (including my own) simply have it wrong. I think the one exception to this rule would be Smarty, although I think it's simply too big, and considering the rest of this article, pretty pointless. There are, however, a couple of reasons why you might choose Smarty (or a similar solution), which will be explored later in this article.

This article discusses template theory. We’ll see why most "template engines" are overkill, and finally, we’ll review a lightweight, lightning fast alternative.

Download And Licensing

The template class and all the examples used in this article can be downloaded here: template.zip. You may use the code in these files according to the MIT Open Source License as published on OSI.

A Little Background on Template Engines

Let's first delve into the background of template engines. Template engines were designed to allow the separation of business logic (for example, retrieving data from a database or calculating shipping costs) from the presentation of data. Template engines solved two major problems:

  1. How to achieve this separation
  2. How to separate "complex" php code from the HTML

This, in theory, allows HTML designers with no PHP experience to modify the appearance of the site without having to look at any PHP code.

However, template systems have also introduced some complexities. First, we now have one "page" built from multiple files. Typically, you might have the main PHP page responsible for business logic, an outer "layout" template that renders the overall layout of the site, an inner content-specific template, a database abstraction layer, and the template engine itself (which may or may not be comprised of multiple files). Alternatively, some people simply include "header" and "footer" files at the start and end of each PHP page.

This is an incredible number of files to generate a single page. Yet, as the PHP parser is pretty fast, the number of files used is probably not important unless your site gets insane amounts of traffic.
However, keep in mind that template systems introduce yet another level of processing. Not only do the template files have to be included, they also have to be parsed (depending on the template system, this can happen in a range of ways -- using regular expressions, str_replaces, compiling, lexical parsing, etc.). This is why template benchmarking became popular: because template engines use a variety of different methods to parse data, some of which are faster than others (also, some template engines offer more features than others).

Template Engine Basics

Basically, template engines utilize a scripting language (PHP) written in C. Inside this embedded scripting language, you have another pseudo-scripting language (whatever tags your template engine supports). Some offer simple variable interpolation and loops. Others offer conditionals and nested loops. Still others (Smarty, at least) offer an interface into a large subset of PHP, as well as a caching layer.

Why do I think Smarty is closest to right? Because Smarty's goal is "the separation of business logic from presentation," not "the separation of PHP code from HTML code." While this seems like a small distinction, it is one that's very important. The ultimate goal of any template engine shouldn't really be to remove all logic from HTML. It should be to separate presentation logic from business logic.

There are plenty of cases where you simply need logic to display your data correctly. For instance, say your business logic is to retrieve a list of users in your database. Your presentation logic would be to display the user list in 3 columns. It would be silly to modify the user list function to return 3 arrays. After all, this function shouldn't be concerned with what's going to happen to the data. Yet, without some sort of logic in your template file, that's exactly what you’d have to do.

While Smarty gets it right in that sense (allowing you to harness pretty much every aspect of PHP), there are still some problems. Basically, it just provides an interface to PHP with new syntax. Stated like that, it seems sort of silly. Is it actually simpler to write {foreach --args} than <? foreach --args ?>? If you do think it's simpler, ask yourself whether it’s so much simpler that you can see real value in including a huge template library to achieve that separation. Granted, Smarty offers many other great features, but it seems like the same benefits could be gained without the huge overhead involved in including the Smarty class libraries.

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Sponsored Links