Home

Bio
My Articles
Blog
Photo albums
Contact me

IT news
Articles


The simplest PHP template engine

14 Jan 2007, 1706 read(s)
Tags:


There are a lot of the so called "template engines" out there on the Internet. They usually provide good support of different functions and are powerful enough to fulfill your requirements. However, in this article I will show you how to create your personal PHP template engine that will work so simple, but so powerful for your small websites.

What is the purpose of a template engine?

A template engine is a piece of software that processes an input text (the template) to produce one or more output texts. The processing itself generally consists in replacing specific sequences of text with data provided by a model or resulting from more complex operations. This actually allows you to easily maintain the whole design and structure of the website without interfering with the content and without having to make changes to every single sub-page.

If you decide to choose a powerful template engine, I would certainly recommend you Smarty. The site of mt-soft.org uses it and we have no problems at all. You should know that the development of a good template system takes a lot of time and programming. That's why choosing a ready one is a good and quick solution. The developers of PHP are good enough to provide you with something really useful.


The simplest PHP template engine

The structure of this engine is as simple as a kid's play. Instead of simply providing direct content within the PHP file using print and echo function, we separate the design of the site from the real content. Moreover, we will provide the content in a variable instead of printing it, and the design - in a separate file, which will handle this variable(s). So, we have a template file for the whole site, usually I name this file design.php. The content of each sub-page will be in separate file - index.php, services.php and whatever else you want.
In the design.php we put our template - standard HTML code, which has its content text and title text removed. Here is an example:

design.php
  1. <html>
  2. <head>
  3. <title></title>
  4. </head>
  5. <body>
  6.  
  7. </body>
  8. </html>


At the place where content and title would have to be we simple put a PHP print command which prints the title and the content of the document:
design.php
  1. <html>
  2. <head>
  3. <title><? print $title; ?></title>
  4. </head>
  5. <body>
  6. <? print $content; ?>
  7. </body>
  8. </html>


And this is it - out simple template file. Where would we know the variables' content as the variables are not even initialized? That's what our subpages will contain - values for the two variables. And we will just have to include the design template in each of our sub-pages. Here is an example for index.php:

index.php
  1. <?
  2. $title="Welcome to my home page";
  3. $content = "Hello and welcome to my home page. This is the content of index.php.";
  4. include "design.php";
  5. ?>


This is it! So simple, and so efficient! Each sub-page will have a similar structure as the index.php file. Moreover, you can also include html tags in the variables, what you must just have in mind, it to escape quotes.
And now, when you have to make simple changes to your whole website's design, just open the design.php template file and make the necessary changes. Everything will automatically be changed for each sub-page.

Improve your template engine

You can also add improvements to your template engine by simply adding more variables and for example using if-s in your template so that you can make more powerful actions like filtering and displaying different content, different styles/navigation based on specific user criteria. You have the full freedom of basing your simple engine to what you want and what your website requires. You can even create several templates for the site and allow the visitors to change it. You will simple have to replace the include value of the end of each file with a variable, not a constant string. But that's a topic for another article, so if I have time, I may write it.

Happy template using!


Comments

Anonymous:
Anonymous:
vladi-minneapolis: I'm BG boy too! I like php *if you want! "lessons": ves@frontiernet.net Any time! We will exchange... php dev. and web dev. This is a my hobby! moge i na balgarski... chao Vladimir

Add comment...