Handling nested blocks within Templates

Abstract

The new template class called \”Integratedtemplate\” by NetUSE is probably, what you have been dreaming of. The author Ulf Wendel <uw@netuse.de> made your dreams come true. Have a look at this introduction sample script. I created two arrays, one containing the section title and another array holding the content. You may of course get your data from a database as well. Pay attention to the parsing of the blocks. Parse an inner block first and then, move to the outer block. Feel free to play around with this example.

Scripts

<!——- main.php ——->
<?php
/**
* Template: Integrated Template by Ulf Wendel
* URL: http://phplib.netuse.de/
*
* Authors: Urs Gehrig <urs@circle.ch>
* Ulf Wendel <uw@netuse.de>
* Date: 18-10-2000
*
* Darstellung im Stil:

club members
muster hans
reuter fritz

customers
muster hans
reuter fritz

others
muster hans
reuter fritz

*/

// … sollte nicht im webverzeichnis liegen. besser:
// include (\”c:/web/phplib/integratedtemplate.inc\”);
include (\”integratedtemplate.inc\”);

$categories = array
(
\”0\” => \”club members\”,
\”1\” => \”customers\”,
\”2\” => \”others\”
);

$adresses = array
(
\”0\” => array (\”name\” => \”muster\”, \”vorname\” => \”hans\”),
\”1\” => array (\”name\” => \”reuter\”, \”vorname\” => \”fritz\”),
\”2\” => array (\”name\” => \”gruner\”, \”vorname\” => \”rudolf\”),
\”3\” => array (\”name\” => \”moser\”, \”vorname\” => \”björn\”),
\”4\” => array (\”name\” => \”amstutz\”, \”vorname\” => \”michael\”)
);

$tpl = new IntegratedTemplate(\”.\”);

$tpl->loadTemplatefile(\”main.tpl.htm\”, true, true);

for($i=0; $i<count($categories); $i++){

// inneren Block _vor_ dem äußeren parsen
$tpl->setCurrentBlock(\”block_2\”);

for ($j=0; $j<count($adresses); $j++) {
$tpl->setVariable( array ( \”name\” => $adresses[$j][\”vorname\”] . \” \” . $adresses[$j][\”name\”] ) );
$tpl->parseCurrentBlock();
}

// äußerer block parsen
$tpl->setCurrentBlock(\”block_1\”);
$tpl->setVariable( array ( \”title\” => $categories[$i] ) );
$tpl->parseCurrentBlock();
}
$tpl->show();

?>

<!——- main.tpl.htm ——->
<html>
<table border>
<!– BEGIN block_1 –>
<tr>
<th>{title}</th>
</tr>
<tr>
<td>
<table border>
<!– BEGIN block_2 –>
<tr>
<td>{name}</td>
</tr>
<!– END block_2 –>
</table>
</td>
</tr>
<!– END block_1 –>
</table>
</html>
Download

The code is distributed \”AS IS\” without any warranty!

Download: http://circle.ch/dl/integratedtemplate.zip (7KB)

Links

http://phplib.netuse.de – The famous PHPLib by NetUSE

published 19.10.2000

Leave a Reply