PHP Class EasyTemplate


Synopsis

EasyTemplate is a PHP Class that handle with template files. This is a very simple class and can be used for small projects and/or use it to develop a new one. For big projects it would be better if you use a more flexible and powerfull template class.

License

GPL-2
Please read the license information before you use this source code!

PHP Versions

The class was developed with PHP Version 4.3.10 and 5.0.3, the class functions with both versions.

Why do I use templates?

You don't need to use templates, if you don't want to. But if you write a wide PHP project you will soon see that using templates makes you manage your project better. Wide projects have a lot of developers and not everybody knows XHTML or PHP (Perl,CGI-Scripts, etc). If you don't use templates then you will have in your scripts PHP and XHTML code as well. In fact, you have to know how the PHP code functions if you want to change the XHTML code and Viceversa. This is not good since the developers not know everything about PHP and XHTML.

There are some people that make very good graphic designs and ar able to create beautiful web styles, the know how to write XHTML and how to present the product, but don't have to knwo how the site functions, or which securities they must built in. Many people are able to write poweful scripts that are safe and execute the code fast and without error, and they don't knwo XHTML or they don't know XHTML as far as the graphic designers and aren't able to present the product in a good way.

Having templates means having separated the PHP code from XHTML. You are able to change the XHTML code without modifying the PHP code, in fact you don't need to know about the PHP code. Changing the PHP Code and implementing more and more powerful PHP code doesn't mean anymore looking for the XHTML code that must be written in a right way, etc. You are able to have independent PHP and XHTML code.

Have you dream about having more than one layout and changing it only with one click? Well, this is very easy with templates and the best of all, you don't have to change anything in the PHP source code.

Download

Example

Well, this is a small example.

The files you need:
  1. class.EasyTempate.php
  2. index.php
  3. templates/page.tpl
  4. templates/table.tpl
  5. templates/rows.tpl
  6. templates/cell.tpl

2. index.php

index.php
<?php
 
function createTable ($arr)
{
    global $tpl;
    foreach( $arr as $key1 => $value )
    {
        foreach( $value as $key2 => $cells )
    {
         $tpl ->assign( 'cell' , 'CONTENT', htmlentities ($cells));
         $tpl-> multiparse(array(array( 'cell', 'row' , 'CELLS')));
    }
     $tpl-> multiparse (array(array('row' , 'table', 'ROWS' )));
    }
 
     $tpl-> multiparse (array(array('table' , 'page', 'TABLES' )));
}
 
 
$info = array(array(array( '12', '15' , '22'),
        array( '20', '18' , '17'),
        array( '12', '89', '-3')),
 
        array(array( 'I', 'You', 'He/She/It', 'We', 'You', 'They' ), 
        array( 'am', 'are', 'is', 'are', 'are', 'are' ),
        array( 'play', 'play' , 'plays' , 'play' , 'play', 'play' ),
        array( 'saw' , 'saw', 'saw' , 'saw', 'saw' , 'saw' )),
 
        array(array( 'Gandalf', 'Saruman', 'Sauron' , 'Aragorn', 'Melkor' ), 
        array( 'Mithrandir', 'Curunír', 'Gorhar', 'Estel', 'Melkoré'),
        array( 'Thor-Kûn', 'Curumo', "Aule's pupil" , 'Thorongil' , 'Morgoth'),
        array( 'Olórin', 'Sharkey' , 'Annatar', 'Elessar', 'Banglir' ),
        array( 'Incánus', 'Chief' , "Melkor's commandant" , 
               'Telecantar ("Strider")', 'Dark Loard' )));
 
 
require_once( "class.EasyTemplate.php");
 
$tpl = new EasyTemplate ("templates" );
 
$tpl->set_templates (array('page' => 'page.tpl',
            'table' => 'table.tpl' ,
            'cell' => 'cell.tpl' ,
            'row' => 'rows.tpl' ));
 
foreach($info as $key => $value )
{
     createTable ($value );
}
 
$tpl-> assign (array(array('alias' => 'page',
             'TITLE' => 'Class EasyTemplate test' ,
             'USER' => 'someone' )));
 
$tpl -> multiparse(array(array( 'last' => 1 , 'page'))); /* parsing the last template */
 
echo $tpl-> get_body();
 
?>

3. templates/page.tpl

templates/page.tpl <html>
<head>
<title>{TITLE}</title>
</head>
<body>
Hello {USER}, <br />
There we have some information for you:
{TABLES}
</body>
</html>

4. templates/table.tpl

templates/table.tpl <p><object>
<table border="1" align="center">
{ROWS}
</table>
</object></p>

5. templates/rows.tpl

templates/rows.tpl <tr>
{CELLS}
</tr>

6. templates/cell.tpl

templates/cell.tpl <td>{CONTENT}</td>

When index.php is parsed then you get this output:

output <html>
<head>
<title>Class EasyTemplate test</title>
</head>
<body>
Hello someone,
<br />
There we have some information for you:
<p><object>
<table border="1" align="center">
<tr>
<td>12</td>
<td>15</td>
<td>22</td>

</tr>
<tr>
<td>20</td>
<td>18</td>
<td>17</td>

</tr>
<tr>
<td>12</td>
<td>89</td>
<td>-3</td>

</tr>

</table>
</object></p>
<p><object>
<table border="1" align="center">
<tr>
<td>I</td>
<td>You</td>
<td>He/She/It</td>
<td>We</td>
<td>You</td>
<td>They</td>

</tr>
<tr>
<td>am</td>
<td>are</td>
<td>is</td>
<td>are</td>
<td>are</td>
<td>are</td>

</tr>
<tr>
<td>play</td>
<td>play</td>
<td>plays</td>
<td>play</td>
<td>play</td>
<td>play</td>

</tr>
<tr>
<td>saw</td>
<td>saw</td>
<td>saw</td>
<td>saw</td>
<td>saw</td>
<td>saw</td>

</tr>

</table>
</object></p>
<p><object>
<table border="1" align="center">
<tr>
<td>Gandalf</td>
<td>Saruman</td>
<td>Sauron</td>
<td>Aragorn</td>
<td>Melkor</td>

</tr>
<tr>
<td>Mithrandir</td>
<td>Curun&iacute;r</td>
<td>Gorhar</td>
<td>Estel</td>
<td>Melkor&eacute;</td>

</tr>
<tr>
<td>Thor-K&ucirc;n</td>
<td>Curumo</td>
<td>Aule's pupil</td>
<td>Thorongil</td>
<td>Morgoth</td>

</tr>
<tr>
<td>Ol&oacute;rin</td>
<td>Sharkey</td>
<td>Annatar</td>
<td>Elessar</td>
<td>Banglir</td>

</tr>
<tr>
<td>Inc&aacute;nus</td>
<td>Chief</td>
<td>Melkor's commandant</td>
<td>Telecantar (&quot;Strider&quot;)</td>
<td>Dark Loard</td>

</tr>

</table>
</object></p>

</body>
</html>

Valid XHTML 1.0 Strict   Valid CSS!