The purpose of this tutorial is to show you how to implement a simple class to handle page templates.
Terms of Agreement:
By using this article, you agree to the following terms...
You may use
this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.
You may link to this article from another website, but ONLY if it is not wrapped in a frame.
You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.
Simple PHP Template Class
The purpose of this tutorial is to show you how to implement a
simple class to handle page templates. It can be extended in any way you
wish.
The Class (template.class.php)
<?
class Template {
public $template;
function load($filepath) {
$this->template = file_get_contents($filepath);
}
function replace($var, $content) {
$this->template = str_replace("#$var#", $content, $this->template);
}
function publish() {
eval("?>".$this->template."<?");
}
}
?>
The Template File (design.html)
This file will contain the design of your web site and the
blank fields that will be merged with content data.
<html>
<head>
<title>#title#</title>
</head>
<body>
<h3>Hello #name#!</h3>
<p>The time is: #datetime#</p>
<? echo "<p>Embedded PHP works too!</p>"; ?>
</body>
</html>
Usage (index.php)
Now we will create a script that load the template and use the
class to merge the data.
<?
include "template.class.php";
$template = new Template;
$template->load("design.html");
$template->replace("title", "My Template Class");
$template->replace("name", "William");
$template->replace("datetime", date("m/d/y"));
$template->publish();
?>
When you run the above script, index.php, it will output the
following:
<html>
<head>
<title>My Template Class</title>
</head>
<body>
<h3>Hello William!</h3>
<p>The time is: 03/10/04</p>
<p>Embedded PHP works too!</p>
</body>
</html>
This code, as you can see, is very simple. It supports
embedding PHP into the original template file (design.html). You could
easily extend it to pull data from a database such as MySQL.
it's because the code is in php5. and i assume you use php4.x which does not support public/private types. By the way, there are ready to use template systems like this, "smarty" for example. (If this comment was disrespectful, please report it.)
Yes, but Smarty is so bloated. I just wanted something simple to merge a page with variables. Smarty is a lot of code to do that, and I've had problems with Smarty's unusual way of caching the design, particularly when I move web hosts. Sometimes the directory will become unreadable and I have to delete it to have it create another. (If this comment was disrespectful, please report it.)
I just wanted to say ...... many thanks for this simple templating system. It was short , simple (If this comment was disrespectful, please report it.)
Thank You Very Much! Simple and to the point and just what I needed! Greatly Appreciated! Also I had to change "public" to "var" as well. (If this comment was disrespectful, please report it.)
That is the BEST and SIMPLEST explaination of template driven PHP code that I have ever read anywhere! I have also read some of your other posts on planet source code and they all Rock! (Specially the JSP / MySQL Tutorial)
Keep up the good work my friend, and thanks again. :D
Pete
(If this comment was disrespectful, please report it.)
10/4/2005 10:32:20 AM:
Brilliant code. I'm trying to understand the simplicity behind the line: eval("?>".$this->template.""); but can't understand why this works so well. (If this comment was disrespectful, please report it.)
There is a litle securoty problem.
HTML:
PHP:
$template->replace("uname", $_POST['user']);
The user suplyed code will be evaluated.
Change # to something more uncommon like {#}
Add this
function clear() {
$this->template = preg_replace('[{#}(.*){#}]', "", $this->template);
}
and call before publish, it will remove all the unused variables.
Peace, GF
(If this comment was disrespectful, please report it.)
Fantastic post. Here’s a tool that lets your build your online database without programming. There is no need to hand code PHP. Cut your development time by 90% http://www.caspio.com/ (If this comment was disrespectful, please report it.)
very useful tutorial for me. I am trying on modifying the code to display some looping row of data. The problem is that I still can't find a way to grab the html content between a particular container, (in this context, I use html comment and to contain the looping section), and then replace the contained part with rows of data. Is it possible to make? Really need your assistance and thank you for writing the article :D (If this comment was disrespectful, please report it.)
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.)