P.E.T.: Processor Engine for Templates
Documentation & Userguide
written by Andreas Demmer
(
andreas@demmer-online.de)
project website:
http://php-pet.sourceforge.net
What Is P.E.T.?
P.E.T. is both a meta-language for designing templates and a engine for processing them. It may be used to
seperate PHP source code from source code written in HTML and therefore, makes your scripts much easier
to handle.
What Can It Do?
It can replace substitute symbols in your templates with content delivered by your PHP script. It is not limited
to single pieces of content but will also work with arbitary interlocked datasets with multiple rows each. You can
use loop tags to define parts of the template, which should be repeated for each row in datasets. Further more,
you can make use of Server Side Includes for as P.E.T. will process them, even if your webserver has no built-in
support for that.
The Meta-Language For Designing The Templates
The templates are not limited to a certain filetype, they can be HTML documents as well as plain text files etc.
The Content Tags
The content tag is used to specify which content should be placed at which position. Insert it into the tempaltes
at the positons where dynamic content should stand. The name of the tag specifies the content.
Example:
Hello <!-- {name} -->, how are you?
|
The Loop Tags
All content between the begin loop and the end loop tag will be repeatet for each row of
an array in the content which is named like the loop.
Example:
<table>
<!-- begin loop {contacts} -->
<tr>
<td><!-- {name} --></td>
<td><!-- {number} --></td>
</tr>
<!-- end loop -->
</table>
|
Loops can contain further loops as long as the content arrays for the loops
contain further content arrays fur the subloops. This allows you to display arbitary
interlocked datasets.
Example:
<table>
<!-- begin loop {contacts} -->
<tr>
<td><!-- {name} --></td>
<td>
<!-- begin loop {numbers} -->
<!-- {number} --><br>
<!-- end loop -->
</td>
</tr>
<!-- end loop -->
</table>
|
SSI: Server Side Includes
SSI tags are used to include the content of other files into the source code. This happens
before the template tags are processed which allows the included file to also contain
template tags which will be processed together with the ones in the hostfile.
Example:
<!--#include virtual="header.html" -->
|
Preparing The Content In PHP
The content is added by a class method "$pet->add_content($content, $name)".
$name is the name of the content tag in the template.
Example:
$pet->add_content('John Doe', 'name');
$pet->add_content('john@doe.com', 'email');
|
The content array will look like this:
$content
[name] => John Doe
[email] => john@doe.com
|
Whole datasets (like a database query result) can be handed over
as well.
Example:
$band[][member] = 'Peter';
$band[][member] = 'Paul';
$band[][member] = 'Mary';
$pet->add_content($band, 'band');
|
The content array will look like this:
$content
[band] => ARRAY
[member] => Peter
[member] => Paul
[member] => Mary
|
Of course, several single tags and several loop tags can be written in one array
The content array will look like this:
$content
[name] => John Doe
[band] => ARRAY
[member] => Peter
[member] => Paul
[member] => Mary
[colors] => ARRAY
[color] => red
[color] => green
[color] => blue
|
Loops within loops can be given content by creating an interlocked content array.
The content array will look like this:
$content
[bands] => ARRAY
[0] => ARRAY
[name] => Peter, Paul & Mary
[members] => ARRAY
[name] => Peter
[name] => Paul
[name] => Mary
[1] => ARRAY
[name] => The Beatles
[members] => ARRAY
[name] => John
[name] => Paul
[name] => George
[name] => Ringo
|
The template for this would look like this:
<table>
<!-- begin loop {bands} -->
<tr>
<td><!-- {name} --></td>
<td>
<!-- begin loop {members} -->
<!-- {name} --><br>
<!-- end loop -->
</td>
</tr>
<!-- end loop -->
</table>
|
This would list each band with all of its members well formatted within a table.
Processing The Templates And Handling The Result
Once created a template and an equivalent content array, it es quite simple to bring
the content into the template.
Example:
<?
$pet = new pet;
$pet->read_file('test.html');
$pet->add_content('Hello world!', 'header');
$pet->parse();
$pet->output();
?>
|
Quite easy, isn't it? :-)