Using templates with PHP

Submitted on: 1/1/2015 6:27:00 AM
By: Gianluca Baldo (from psc cd)  
Level: Intermediate
User Rating: By 5 Users
Compatibility: PHP 3.0, PHP 4.0
Views: 679
     The article explains a personal and simple approach to the use of templates in PHP scripts.


Using templates with PHP

We will assume you are planning to develop a PHP application.
First question: what are templates?

Templates are text files usually containing HTML code and "some tags" to fill the HTML with dinamically generated content. Mantaining your PHP code separated from the files responsable for the visual part of your site, will give the ability to change your site's look & feel in a simple and fast way in the future, without having to touch you PHP code.

Embedding HTML in your PHP is not a good idea. The more you keep your HTML independent the easier it will be to maintain you site.
An approach like the following, having a PHP file which outputs the HTML tags (we assume you have to output the content of the variable $test):

[some HTML here] $test [some more HTML]

"; ?>

is not convenient since changes to your HTML will require that you edit your PHP code. This approach can be somehow confusing and not confortable.
A better solution could be:

[you HTML here]

[some more HTML]

which is not a template yet, but we are beginning to separate the HTML tags from PHP code.
If you need to perform some actions before your to give the variable $test the correct value (i.e. accessing a database table and reading a column value) you'll have to add the necessary PHP code, for example at the very top of the file. Something like:

$result = mysql_query($query);
$test = mysql_result($result,0,"myvalue"); ?> [you HTML here]

[some more HTML]

So we now have a unique PHP file where we intend to keep HTML tags and PHP code separated. This is simpler to mantain and will be clearer to any other who will have to edit and/or mantain your script.
HTML tools like Dreamweaver can recognize PHP tags and will not alter your PHP code. I use to develop PHP applications where the visual part is rather complex and Dreamweaver is a good tool to help in the production process.
So if your WYSIWYG HTML editor do not alter PHP code, you can open your script and just make the necessary changes to the HTML. The PHP will be generally shown as an icon and you can simply move the code behind that icon (if necessary) moving the icon itself.

This sounds good enough, so: why should you use templates?
Let's see how templates works (well, the way I use templates) and this will probably answer the question.

First let's create the tamplate file (i.e. sample_template.html). It will contain HTML tags we need to create the visual part of our script.

[you HTML here]

After that, let's create the PHP file which will contain the PHP code we need:

$result = mysql_query($query);
$test = mysql_result($result,0,"myvalue"); include "sample_template.html"; ?>

We now need to make the last change to our HTML file:

[you HTML here]

What we basically did is to separate the PHP code and the HTML in two different files. The PHP script file performs the necessary actions and, once done, includes the template (HTML) file.
This is really better than the "one-file" solution since the HTML is clear and even graphic designers can open the template files and make visual changes to them, being sure they will not accidentally alter the PHP behind it.

There are many approaches to the templates idea, the one I presented here is the one I found more confortable after using some of them.
Here are some reference if you want to read something more about templates.

Templates - why and how to use them in PHP3 (by Sasha Shumann)
Templates, The PHPLIB Way (by David Orr )
Using Templates (by Todd Williams)
Includes and Templates with PHP

Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Intermediate category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)

Other User Comments

 There are no comments on this submission.

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)

To post feedback, first please login.