Template Engines Fundamentals for PHP

Today, I thought I would change the pace a bit and dive into something a little more on the technical side: PHP Template Engines. For those of you who are not programmers, let me give you a basic rundown what I’m talking about. Let’s take a PHP-driven website. The content is being created dynamically, probably be being pulled out of a database on-the-fly. The PHP code on the site has to take that data out of the database and insert it into the website’s template. When this is done, the website has the look it should. However, when one looks at the source code for that PHP, the very messy way to do it is to have your layout mixed in with the code which actually queries and processed the data for you. PHP allows you to embed code right into an HTML file if you wish, but the problem is that doing this means that changing the layout later can be a REAL chore – you would have to update every single script on the site. Another problem is that the design and the code is all mixed together, so you cannot delegate tasks properly. A designer probably doesn’t know programming, and vice versa.

What a template system does is allow you to separate the site’s design from the underlying PHP code. By placing certain syntax into the template such as placeholder variables, blocks, and includes, the designer can create the layout separately from the programmer. And the other beautiful thing is that you can change the templates in one place and the entire design of the site can change in one swoop. This makes maintenance MUCH easier.

Let’s take Miraserver, for example. Miraserver uses a template system called TemplatePower. The templates themselves reside in the database, but you can edit the design of the website via the control panel. On the public site, all of the processing is done by PHP code and that layout is pulled in, processed, then output to the user’s browser as a complete webpage. You can control the look and feel of the website globally. Plus, you can control the design of the website without having to hack up the underlying PHP code. And that is the whole point – to separate presentation from programming.

The General Picture

Most template systems have some basic similarities. The actual syntax which you put into the templates varies from system to system, however most operate by replacing variables with their assigned values, including blocks, looping blocks, etc. A block, in a template, is simply a segment of HTML code which is set aside and given a block name. Then, with PHP, you can call that block or hide it. If the block is never called in, the user will never see it. If it is, it will show up. And if you use a loop in programming, you can call the block over and over again. For example, let’s say you have a table of data to output. You could make one table row in that table, make it a block, then use PHP to simply display it over and over again. The finished page will be a table full of information.

So, let’s give you a basic table layout for a template:

< table cellpadding="0" cellspacing="0" >
< !-- START BLOCK : ROW -- >
< tr>< td>{column1}< /td>< td>{column2}< /td>< /tr>
< !-- END BLOCK : ROW -->
< /table>

Now, this template is using the syntax for TemplatePower, but many other systems will have similar syntax. You can see that this is a basic table, but that the row is shown only once while being surrounded by block names that define the start and end. When I create a programming loop in PHP to show my results, that block called ROW is called over and over again.

Other typical template features are including files, reusing blocks, outputting content to a variable, etc. Also, depending on the template system being used, you can find logical capability right in the template itself. For example, if a particular variable equals x, show this part of the template. There are many times when having some basic logic capability right in the template can be a lot easier and cleaner than trying to do it all in the PHP code itself.

The Options

Ok, so now that everybody knows what I’m talking about, let’s dive into the more technical stuff – the options. There are a lot of pre-written template systems out there to choose from. Many choose to use an existing solution. Some choose to create their own to suit their own particular needs. Here is a list of solutions for you to browse through. You programmers will probably find this the most valuable portion of the article. :-)

There are a lot more, too, believe it or not. I have been using TemplatePower for most of my projects lately. It is simply a matter of including the class file and using it. Very simple. And it is fast. It does have some limitations such as the lack of logical capability in the templates – but these are features which if not properly can slow it down. So, it is a tradeoff. TemplatePower is not the best out there at all – it just happens to be what I have been using lately because of its ease of use.

Conclusion

If you are about to develop a new website or are about to re-do one that already exists, I recommend you look into using a template system of some kind. I have been working online long enough to have made some good mistakes. One of the biggest was developing a large site without using some kind of template system. When I later went to change the layout, you can imagine what a pain that was. A day full of copy/paste actions in Microsoft Frontpage (which is what I was using back then). I later expanded my horizons and developed a content management system (which later turned into Miraserver) and used a template system. It makes site maintenance SO much easier.

Like This Post? Join Our Private Newsletter (it's free):

Your Email:

Want Technical Help With Your Blog?
What if you could work on your business, rather than fighting Wordpress?

HelpYou can sit around paralyzed by your lack of tech knowledge, or you can grab a life-line.

David offers a tech workshop where he hand-holds people through the various technical hurdles of using Wordpress. He'll answer your questions, and put his technical experience to work for you.

The goal? You being able to work on your business, rather than fighting annoying technical problems.

x

10 Mistakes You May Be Making Right Now With Your Blog


How To Avoid 10 HUGE Mistakes Most Blog Owners Are Making Without Even Knowing It!


Join our private newsletter, and get a complementary copy of Blogging Gone Wrong.

Email:

testing