Home / code / php

Templates in PHP

With templates in PHP you can completely separate the (PHP)coding and the design (HTML).
This is very handy if you have a different programmer and designer.

If you want to use templates you need class.fasttemplate.php.
You can download it here (zipped). Or get the latest version : http://www.thewebmasters.net

Note when I installed PHP Version 4.3.1, I had a problem with class.fasttemplate.php on line 638.
Here is the fixed version : class.fasttemplate.php.

Note when I installed PHP Version 5.3.3, I got an error that the class wasn't found.
Here is the fixed version : class.fasttemplate.php.

The code for PHP-page

<php;
include_once("class.fasttemplate.php");
/* the path to the class.fasttemplate.php file */

$myTemplate = new FastTemplate(".");
$myTemplate->define(array(
	'main' => 'template.tpl'
));
/*here you say that the template.tpl (the HTML file) is now known as main.
Later you will parse the data in to it */

$title = "Here you place the title";
$body = "<p>here you place the text.</p>";
$body .= "Add somemore text";

$myTemplate->assign(array('PAGE' =>$body, 'TITLE'=>$title, 'BOTTOM'=>'copyright'));
/* Here you give a name to the variables.
It is this name that will be used in the HTML-file */

$myTemplate->parse('PAGE','main');
$myTemplate->FastPrint();
/* Here you parse and print it */

Below is an example code for the template.tpl

<HTML>
<HEAD>
 <TITLE></TITLE>
</HEAD>
<BODY>
<h1></h1>

{PAGE}

<small>{BOTTOM}</small>
</BODY>
</HTML>
As you can see the name of the "variables" are always placed between {   }.

If you want you can also combine several templates. (one main template, and a template for the rows of a table, ...)

 

TOP

Latest script:

 

Books: