PHP and template engines (and some other stuff)

I’m developing web sites for quite some time now, started when i was… Young? Yellow? Well, started a long time a go in a galaxy… AARGH! Can’t get Star Wars out of my life! Anyway. In the beginning i was using the good old php mess approach:

echo 'ul';
foreach($array as $item) {
    echo '<li>Some $item here</li>';
}
echo '<ul>';

And so on. For onepagers this was enough. So i stumbled accross mambo (former Joomla) and saw the raw power of cms and, well for me, good coding. K, let’s make ourselves a content management system. But how? What do we need? The neccesary ingridients where these:

  • Apache with php (obvious)
  • MySQL database (oh, what’s this database thing for?)
  • foresight

What’s the most important? No, wrong, not coffee. Foresight! Without analyzing and planning the outcome can’t be good. I started to code. Heavily. Took a good number of different approaches, all ended bit dull and like they couldn’t take on serious tasks.

Why this intro? So you can better relate to me. I AM one of you, coders. We all started like this. One learns trough he’s own mistakes. What have I learned? Let’s take this a bit further. There is no good CMS without know-how, so these are the things one has to be familiar with:

  • apache web server and its .htaccess or http.conf files and configurations. Hence the readable links, good caching, security…
  • Linux, yes, you need a bit of knowledge of that OS and it’s abilities. Good start is to install it on an empty partition or run it virtualized with VMWare. When you get to know it, you can do marvels like putting a cache directory into system memory with tmpfs, and so on
  • php programming language, with good knowledge of object oriented programming (OOP). This just simplifies things, realy.
  • usage of a template engine (oh, we’re getting on to a topic here, let’s have a beer!). I prefer Smarty, some say it’s too big, too complicated, too **insert your reason here**. I found that I can do my sites faster with it, so i use it
  • mysql database. This could be the hardest and most time consuming, but it’s well worth it. It includes database designing and SQL programming
  • benchmarking and debugging is also a part of the process. I use bit of xdebug, zend platform, smarty debug console, my own debug console for page creation time and sql execution times

Ok, this is a handful, but any of the above items should be tackled at some point of the learning curve. If we are trying to code and produce scalable sites that can do the task right, we should be familiar with the MVC architecture. It comes down to having a database to store our data, php code to process that data and template to show that data. The upper code with all this in mind would be:

DATABASE:

1. item 1
2. item 2
3. item 3
4. …

PHP and SQL:

$data = $db->fetch('SELECT item FROM table');
$t->assign('data', $data);

TEMPLATE:

<h1>Our items</h1><ul>
{section name=item loop=$data}<li>{$data[item]}</li>
{/section}</ul>

Here we use a database abstraction layer class ($db) and are free of repeating the same change accross our code should we change database, etc. The template is free for editing by the designer, and the coder can still work on his code. Now we have everything separated, and one process can be changed regardless of the other.

One question pops up when we see {section}. It’s smarties way of saying “loop array $data and present data in the following way”. So, why should our designer be bothered with foreaches and if-then-elses? Answer lies in the following lines:

{section name=item loop=$data}
<li {if $smarty.section.item.first}class="first"{/if}>{$data[item]|upper}</li>
{/section}

Designer just realized that the data presented is not right. Items should be uppercase and the first <li> should have class=”first”. Instead of yelling to the programmer “give me uppercased items!!!”, the designer has the power to modify, mold the data. Php guy is working on the control segment as we speak, he is half through the form processing code, which takes the order or whatnot, and the designer is at liberty to change the appearance of the data. He will choose to truncate long text, format date to display only day and month, maybe put {sectionelse} No data to display {/section} if $data array is empty, and he will never need to bother the programmer for those tasks.

If the caching is turned on, the php code and database calls will never even be executed, because the content is already rendered in some static file (which resides in our cache directory happily mounted to memory with tmpfs)

As we can see, this architecture helps alot, but it can be moved further. What if you need to present this data a couple of times across your site? It would be good if we could somehow include this block anytime we need it. And so we can, with {include file=”items.tpl”}. Anywhere we need this data (on the homepage, in the cart…) we just include this bit of a line. Saves time, eh? Now for the bad part.

Drawbacks

Template engines are good things to have, but they slow execution of the page. Smarty renders php code out of the template, and for that needs some processing time. He renders only once, and the rendered template benefits from the usage of some opcode cache module like eAccellerator, Zend Optimizer and such.

There is also the need to learn new language, smarty’s template language. It’s not that hard, and there is a lot of documentation on the net for it. When you get accustomed to your template engines language, and start coding, the html starts to look really ugly. That’s really up to you to decide weather you want to have all the foreaches and if/then/elses in the php, parse them, and send them to the template. There is also PHPTal template engine, whose code is really simple and intuitive.

Conclusion

Once you take the MVC road, there is no turning back. You will never go back to having all the code and html in one file. Your next stop is maybe using some frameworks to get the job done even faster (symfony, akelos, zend framework…), or building your own classes for your application.

Keep coding!

  • kl

    PHPTAL also compiles templates to PHP, so it’s very fast.