EasyTemplate logo Skip navigation? Download (.ZIP)
Content starts.


EasyTemplate is a set of functions and HTML scripting techniques, that make it easier to develop low level PHP solutions. Scripting solutions done with EasyTemplate can be maintained by HTML developers without any PHP knowledge.

EasyTemplate is not another PHP templating language like Smarty, developers do not have to learn any new tags or command strings and there is no programming logic in the HTML files.

All the functionality of the PHP script is triggered via HTML comments.

This is not a new idea, there has been a discussion on this issue on PHPBuilder a long time ago, and loads of emails and messages were exchanged on the subject.

Many other developers also came up with solutions, but most of them required either to add invalid markup into the HTML template, or the PHP functionality was rather advanced.

EasyTemplate targets PHP Developers that produce scripts that will be used by HTML developers and agencies that do not want to know about programming logic, but rather concentrate on developing nice HTML. One of the requirements for being able to do that, is to be able to see the changes you do in your HTML without using the PHP backend.

Back to Menu

Why EasyTemplate

One of the main reasons why PHP is so successful is it's low learning curve. Any HTML developer with some insight into JavaScript or PERL is able to learn it fast.

The ability of PHP to be mixed with HTML leads to the first scripting attempts being satisfying and that is why there is a lot of PHP code out there mixing markup with logic.

This makes "real" programmers shiver and see PHP just as another one scripting language, and not to be taken serious.

Mixing display and logic is fast, easy and also makes debugging easy, but is poison, should a change of layout be necessary.

For example this bit of code:

echo "<table>"; while ($row = mysql_fetch_array($sql_result)) { echo "<TR"; $i++; $rowcolor=$i%2==0?'#cccccc':'#eeeeee'; echo " bgcolor=\"$rowcolor\">"; echo "<TD>".$row['NAME']."</TD>"; echo "<TD>".$row['BIRTHDAY']."</TD>"; echo "<TD>".$row['DEPARTMENT']."</TD>"; echo "</TR>"; } echo"</table>";

Is perfectly understandable to a PHP developer, but an graphical design oriented HTML developer has a real problem changing anything in there.

As a Front End Developer, I do not want to care about how to retrieve the data, or what logic has to dictate what gets displayed.

As a PHP developer, I do not want to care, how some subtle change in the site is done, or I don't want to be asked to do changes in copy text.

By using EasyTemplate, the same result as above can be achieved by the following, completely separated PHP and HTML documents.

PHP: include_once('easytemplate.php'); $HTML=load("template.HTML"); while ($row = mysql_fetch_array($sql_result)) { $i++; $rowHTML=$i%2==0?getHTML($HTML,'rowcolor1'):getHTML($HTML,'rowcolor2'); $rowHTML=setVar($rowHTML,'name',$row["NAME"]); $rowHTML=setVar($rowHTML,'birthday',$row["BIRTHDAY"]); $rowHTML=setVar($rowHTML,'department',$row["DEPARTMENT"]); $incHTML.=$rowHTML; } $HTML=replaceHTML($HTML,'rows',$incHTML); echo cleanup($HTML); HTML: <table> <-- start:rows --> <-- start:rowcolor1 --> <tr bgcolor="#eeeeee"> <td>%%name%%</td> <td>%%birthday%%</td> <td>%%department%%</td> </tr> <-- end:rowcolor1 --> <-- start:rowcolor2 --> <tr bgcolor="#cccccc"> <td>%%name%%</td> <td>%%birthday%%</td> <td>%%department%%</td> </tr> <-- end:rowcolor2 --> <-- end:rows --> </table>

Or, if the HTML template is meant to be shorter, you can use parameters:

PHP: include_once('easytemplate.php'); $HTML=load("template.HTML"); while ($row = mysql_fetch_array($sql_result)) { $i++; $rowHTML=getHTML($HTML,'row'); $rowcol=$i%2==0?getHTMLpara($HTML,'rowcolor1'):getHTMLpara($HTML,'rowcolor2'); $rowHTML=setVar($rowHTML,'color',$rowcol); $rowHTML=setVar($rowHTML,'name',$row["NAME"]); $rowHTML=setVar($rowHTML,'birthday',$row["BIRTHDAY"]); $rowHTML=setVar($rowHTML,'department',$row["DEPARTMENT"]); $incHTML.=$rowHTML; } $HTML=replaceHTML($HTML,'row',$incHTML); echo cleanup($HTML); HTML: <-- rowcolor1:cccccc --> <-- rowcolor2:eeeeee --> <table> <-- start:row --> <tr bgcolor="#%%color%%"> <td>%%name%%</td> <td>%%birthday%%</td> <td>%%department%%</td> </tr> <-- end:row --> </table>

At first glance, it looks complicated, but it isn't. Once both developers got used to the function names and the comment syntax, building modular scripts and pages is very easy.

The HTML developer has complete control over the layout, all the HTML can be developed and checked in a browser, the PHP developer can concentrate on the programming logic and populating the variables in the HTML.

Once this logic is finished, any HTML display change can be done without the need for a PHP savvy developer.

It is a technique of taking PHP from the sloppy way of scripting to a better, cleaner and more logical level, and all this just by reusing some functions.

Back to Menu


These are some examples how to use EasyTemplate.

Example 1 (HTML#1) (PHP#1)

This example shows how to create a small Login/Namecheck screen. A form is displayed and the entered Name compared to an admin name in a HTML parameter. If the name is the admin's name, an admin link gets displayed, if not, a simple welcome message.

Example 2 (HTML#2) (PHP#2)

This example shows how to create a results table with alternate colours in each row. The items to display, the amount of items displayed each time and the colours are defined via parameters. HTML sections define the next and previous links, and the row to display for each item. The PHP computes all the necessary logic.

Example 3 (HTML#3) (PHP#3)

This example shows how to create a formmailer. The Form, the return page and the email body get defined via HTML sections.The email subject, where it came from, where it gets sent to and the mandatory fields in the form are defined as HTML parameters. The mail() function is disabled in this example, simply uncomment the mail() line to make it work.


EasyTemplate is free for use at the moment, given great acceptance, I plan to move it onto sourceforge and make it Open Source.

For the moment please keep in contact if you plan to do any modifications.

Back to Menu


You can download this instructions file, the demofiles and EasyTemplate as one zip file by clicking the link below.

Download EasyTemplate (.zip - 15 KB)

Version 1.0 21.January 2003

Back to Menu

The HTML Templates

The idea of EasyTemplate was to allow developers with HTML knowledge to define elements of the page which get displayed or not according to what the programming logic dictates.

Another requirement is paramaters, that will be used by the PHP script.

Furthermore there was a necessity for variables, which will be populated by the PHP script.

To keep all of this as easy as possible for the HTML developer, most of this functionality is achieved via HTML comments. These have the advantage that they are not displayed, which means the developer can see the page as it will be without any disturbing special tags or characters.

Back to Menu


These are settings in the HTML template, that the PHP script will read and use, their syntax is:

<-- name:setting -->

For example <-- Items:5 --> sets the amount of "Items" to "5", <!-- owner:admin@foo.com --> sets "owner" to "admin@foo.com".

Beware: Parameters have to be on one line. No linebreaks are allowed.

Back to Menu

Logical markup sections

These describe parts of the document, that are displayed or not, dependent on the logic in the PHP script.

<-- start:section --> ...HTML code... <-- end:section -->

For example:

<-- start:Error --> <p class="boo">You forgot to enter the correct name.</p> <-- end:Error -->

Will be available to the PHP logic as "Error".

Beware: Sections have to be on several lines, without linebreaks replaceHTML will not work.

Back to Menu


Variables are parts of a markup section or the document that the PHP logic replaces with the actual data. These are not defined as HTML comments, to allow the HTML developer to use them in attributes.

<div class="%%class%%> <a href="%%link%%" title="%%title%%">%%linkdescription%%</a> </div>

Each variable is defined by surrounding double percentage characters. You can use variables with the same name in different sections of the page, unless the PHP developer does not do a global replace, this will not affect the logic.

Back to Menu


To allow commenting of the EasyTemplate templates, start the line with a hash(#).

<-- # This is a comment -->

These comments will be deleted when EasyTemplate prints out the template. Normal HTML comments without the hash(#) stay as they are.

Beware: Comments must be on one line.

Back to Menu

The PHP functions

EasyTemplate works with a set of really small functions, that are not more but some regular expressions. Each functions deals with a different problem and/or HTML template command.


This function replaces the variable var in doc with the set value.


Replaces the HTML variable %%linkback%% with the refering page.


Replaces the HTML variable %%offset%% in the HTML section "display" with "300" and populates the variable $display with the resulting HTML.

Back to Menu


This function reads the value of the HTML parameter sw in the document doc.


Reads the value of the HTML parameter amount and populates the variable amount with it. The parameter <-- amount:20 --> would result in amount becoming 20.

Back to Menu


This function reads the HTML sectionelm in the document doc.


Reads the HTML in between <-- start:error --> and <-- end:error --> and populates the variable error with it.

Back to Menu


This function deletes the HTML sections elm in the document doc. Separate the sections with a comma.


Deletes the HTML in between <-- start:error --> and <-- end:error --> in the document doc.


Deletes the HTML in between <-- start:form --> and <-- end:form --> and the HTML in between <-- start:form --> and <-- end:form --> in the document doc.

Back to Menu


This function replaces the HTML section elm in the document doc with the variable new.

$doc=replaceHTML($doc,'error','There are no errors to report.');

Replaces the HTML in between <-- start:error --> and <-- end:error --> in the document doc with "There are no errors to report.".

Back to Menu


This function deletes all EasyTemplate comments and parameters in the document doc.

echo cleanup($HTML);

Could be the last line in the PHP to display the results.

Back to Menu


This function loads the file "file".


Populates "HTML" with the content of the file "template.HTML". If the file does not exist, HTML will be empty.

Back to Menu


This function stores the content of "newdatas" in the file "filelocation". If the storing fails, the function returns "1",otherwise "0".

$sure=save('template.HTML',$HTML); if ($sure==1){echo 'There was an error saving the data!';}

Beware: To allow save() to store the data, the document needs to have the correct filepermissions set.

Back to Menu

Validity:  Valid XHTML 1.0  Accessibility:  Approved AAA  Approved 508