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):
echo "
[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] echo $test; ?> [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
echo $test; ?> 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:
// We assume your are running MySQL
$query = "SELECT myvalue FROM test_table"; $result = mysql_query($query); $test = mysql_result($result,0,"myvalue");
?>
[you HTML here] echo $test; ?> [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:
// We assume your are running MySQL
$query = "SELECT myvalue FROM test_table"; $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]
echo $test; ?>
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
|