View Full Version : SimpleTemplates: A guide to making a simple template system in PHP


iTom
04-01-2007, 06:52 PM
PHP is amazing. If you want to get to know it, see the website (http://www.php.net/) (php.net). It's a web scripting language with so many possibilities. One of these possibilities is templates. You can make easy templates, backing them with a database using MySQL and many other things. I bring you: SimpleTemplates!

What's the deal with SimpleTemplates?
SimpleTemplates was an idea I clocked onto about a month ago. It struck me like a penny falling from the top of the Empire State building. I needed a way to do substitution with variables and implement certain tags, like <st:if> or something to show data, without using PHP tags, because they open a security hole. Plus, in contrast, usually the templates are far smaller.

In all simplicity the system works by using eval(), addslashes() and a few other clever functions. As you probably know, $variables in strings are replaced by the respected values. I decided use this to my advantage. I can also do some replacing to get the <st:if> tags working.

In these tutorials I will be teaching you how to create a SimpleTemplates (ST) script and a template, then we will be going onto <st:if> (and maybe other control structures!) and then back it up with a database.

For example, you could:

<html>
<head>
<title><?php echo $blahtitle; ?></title>
</head>
<body>
<?php echo $blahtext; ?></body>
</html>


But it would be more appealing, secure, smaller and easier to use the following code:

<html>
<head>
<title>{$blahtitle}</title>
</head>
<body>
{$blahtext}
</body>
</html>

(Some members will point out the brackets around variables in strings are not neccessary, but I like to code variables in my templates like that. In code or in other strings I don't normally do that. This makes them stick out much more and potentially means a highlighting script for templates like the one above would be easier to make)

Considering that $blahtitle might be "Hello, world!" and $blahtext might be "<b>Hello, world!</b>", both would generate the same HTML (when combined with ST), but the length and ease of coding is significantly reduced.

<html>
<head>
<title>Hello, world!</title>
</head>
<body>
<b>Hello, world!</b>
</body>
</html>


In the next post I will be explaining how to do this.

iTom
04-01-2007, 07:44 PM
So, how do we utilize these ÜberTemplates?
You've probably guessed from the hint I dropped you about eval(). There are a few functions that I've written which do the stuff we need. These functions only require PHP 4; but note that this is the simplistic version; it does not fetch any templates from a database and doesn't parse <st:if> or other control statements at the moment. If you want the more advanced version, read on.


<?php
/**
* SimpleTemplates version 1.0
* (C) Copyright 2007 Thomas Oldbury
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

// Fix up a template. At the moment all it is an addslashes
// function, but that will change with later revisions.
function st_template_parse($templatecode)
{
return addslashes($templatecode);
}

/**
* TO PARSE A FILE use the following code:
* -------------------------------------------------
* $parseresult = eval('return "' . st_template_parse($templatecode) . '"');
*/

/**
* EXAMPLE USAGE:
* -------------------------------------------------
*/

$blahtitle = "Umm... ";
$blahtext = "Hello, world! "

// ALWAYS USE SINGLE QUOTES!!
$templatecode = '
<html>
<head>
<title>{$blahtitle}</title>
</head>
<body>
{$blahtext}
</body>
</html>';

$parseresult = stripslashes(eval('return "' . st_template_parse($templatecode) . '"; '));

echo $parseresult;
?>


The resulting output is:

<html>
<head>
<title>Umm... </title>
</head>
<body>
Hello, world!
</body>
</html>


But wait a minute? Why not just use echo "$blahvar etc";?
This code has it's advantages to using double quotes. First, you can use a database backend, be it MySQL, a flat file or even an on-the-fly generated piece of code. Thats one of the best advantages. Also, in the next post we will be implementing the <st:if> control structure.

iTom
04-01-2007, 08:17 PM
So what's an <st:if> statement?
An <st:if> statement, or simply a if statement is a special control structure which, based on a condition specified, will show only certain code. The reason for the namespace* is to stop conflicts. An if statement goes like this:


<st:if>
<if>$condition == true</if>
<then>$condition is true</then>
<else>$condition is false</else>**
</st:if>

* Those who are experienced in XML will probably know what a namespace is. For people that don't, it's simply a subset of tags. E.G. consider a popular (unnamed) browser company wants to implement a new feature. Their tag name conflicts with another or a standard tag. What's the answer? Namespaces. Text before the colon is a namespace. Tags without a namespace are in the general tag set. The reason I've used the namespace st: is to avoid a possible name clash, with say, an XML document (you can use these in XML, you know!)

** The else clause is optional and defaults to empty.

Wow! That sounds cool! How do I do it?
At the moment I'm still working on this. I had a script that did it before, but, luck would have it, I've lost it.
So, at the moment, please stay tuned!