PHP Class EasyTemplate
Home | University | GNU/Linux | Projects | Impressum |
- Safe Strings project
- Generate Background project
- PHP Class EasyTemplate
- PHP Class SqlQuery
- AOF-DB Project
- AOF-GUI Project
- SommerCampus 2006 :: CStrings
- SommerCampus 2006 :: Linux/Unix system programming
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:
2. index.php
index.php
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ír</td>
<td>Gorhar</td>
<td>Estel</td>
<td>Melkoré</td>
</tr>
<tr>
<td>Thor-Kûn</td>
<td>Curumo</td>
<td>Aule's pupil</td>
<td>Thorongil</td>
<td>Morgoth</td>
</tr>
<tr>
<td>Olórin</td>
<td>Sharkey</td>
<td>Annatar</td>
<td>Elessar</td>
<td>Banglir</td>
</tr>
<tr>
<td>Incánus</td>
<td>Chief</td>
<td>Melkor's commandant</td>
<td>Telecantar ("Strider")</td>
<td>Dark Loard</td>
</tr>
</table>
</object></p>
</body>
</html>