Note: this is article is cut in parts, this is the only the first one. More to come.

Programming in PHP without separating content and code can lead to unwanted results, low mantainability and slower production. Even nowadays you can find tons of scripts written by filling HTML pages with chunks of code. If you do that,

  • Web designers with no programming skills will not be able to customize easily your script’s appearance.
  • If you want to change a site, for example a festivity or a redesign, you will need to face a big amount of work adding again all the PHP code to the new HTML pages.
  • You will not be able to focus on the project because you’ll have to manage yourself all the aspect of HTML production, from loops to file inclusion.

At my first web works I had these problems too. Building a new site required more work that I expected, because it was also difficult to reuse the code built for preceding customers. Then I started putting all my HTML in separate files, reading it with PHP and doing substitutions where needed. That was really better, I was able to change HTML when I wanted, but nothing more. Data that needed to be presented by "looping" the same HTML needed to be built in PHP, and in some situations I needed some sort of conditional programming in these so called ‘template files’. I started to write a full fledged ‘template engine’ myself when a project saved me. I customized an e-commerce script for a project, and that script used an open source template engine. At first I was… how to say? Puzzled! As I looked over the template files I saw heaps of strange code.. it wasn’t PHP so I tought that It was a really bad idea to push the things so further. Half of the script functionality lied in the template files themselves, and I was even a bit angry because I tought I had to learn a new language… inside the language!
But as I was changing the look and feel of that site, so was my opinion of that system. It’s not bad after all… no, it’s good indeed.. wait, it’s great! I never went back and used that engine myself for all my other projects: it’s name is Smarty.
Here are some interesting functions:

  • You can edit the template files as if they are HTML documents and separe them from the code with a little effort, if you don’t want to learn the Smarty language.
  • Smarty language is much natural and template oriented than PHP, some things like iterate an array and outputting the same HTML code with the array data inside are very easy to do.
  • You can include other files from your templates, these files will be parsed too.
  • There’s support for variables and constants
  • You can produce a piece of HTML, then save it into a variable and reuse it later in the page, even modify it.
  • You can put decisional code in the templates, do math operations and store results in new variables.
  • Smarty has a plugin system with a very good repository, it’s easy to write new plugins and they are autoloaded after you put them into their folder.
  • Has an advanced cache system that builds already decoded chunks of templates for speed purposes, so you actually can go even faster than mixing PHP with HTML.
  • There are many output buffering functions that give you abilities to change the whole page after it is generated and before is sent to the client, just for example you can with a single line change all your website and tell the system to add target="_new" to all external links.
  • You can "skin" your site as you want, it’s really possible to do a complete makeup of a whole web project in a day or two, even change skins in realtime!
  • There’s a template debugger to show data being passed to the engine!

So, by now you would think that Smarty is the best thing after chocolate milk, but it has some negative aspects worth exploring:

  • All the HTML is sent at the same time, while with PHP you can send the page in chunks. However that required special attention when you wanted to set cookies, because these need to be sent before everything else.
  • Smarty is written in PHP itself. A C++ templating engine maybe would be faster (there are some out there), but then you would need to write new plugins in C++ too. You use smarty by actually loading it entirely in memory each time you need to access template files. If you need to output really simple HTML code in little pieces of code, and these pieces are in separate pages, don’t use it. Just an example, if your script just outputs "OK" after the computations, it’s better to use echo for the text.
  • You will need to learn a new language. It’s simple and everything, but it’s not rich as PHP itself, it’s only a template language. The thing I miss more is the switch conditional construct, you need a series of if..elseif…elseif to emulate it. However this language is always used only to produce the presentation, you’ll end up moving everything related to visualization to the templates, never using echo again if not for debug purposes.
  • It’s not always easy to upgrade an existing project to using a template engine.
  • If you use an aggressive caching mode and make changes to the templates, those will not reflect online until you clean the template cache directory.
  • Smarty adds memory to your requirements, better have a look at the PHP memory_limit setting.
  • It has some good builtin plugins to produce drop downs listboxes, radio buttons and dates inputs in forms, but these elements require formatting the data in a certain way.
  • If you dive other scripts directly into the page, some symbols, like the curly braces, can collide with Smarty, that will think you are initiating a template command sequence, so you’ll need to surround these pieces of code with literal tags, that tell Smarty not to parse them.

As this argument is very long, I decided to cut it in parts. Next part will be about installing Smarty and starting using it. We’ll also have a look at other template engines, and talk about full PHP frameworks that use the MVC paradigm, but don’t worry about that right now.