KTemplate - Examples
Tags: KTemplate PHP
Have a look at this template file:
<html>
<head>
<title>
KTemplate Template Test
</title>
</head>
<body bgcolor="#ffffff">
<h3>Simple replacing</h3>
<p>
<li>replace with a string:</li>
{content}
</p>
<h3>Single block</h3>
<ul>
<!-- BEGIN temptest -->
<li>
{item}
</li>
<!-- END temptest -->
</ul>
<h3>Nested blocks</h3>
<!-- BEGIN table -->
<table border="1">
<!-- BEGIN row -->
<tr>
<!-- BEGIN col -->
<td>
{cell_content}
</td>
<!-- END col -->
</tr>
<!-- END row -->
</table>
<!-- END table -->
<h3>Put any content you want into another placeholder</h3>
(here: Repetition of complete table.)<br>
{my_content}
</body>
</html>
Here is the commented PHP code which creates this output.
<?php
include "/vol/www/oovebobe/KTemplate/class_ktemplate.inc.php";
// create instance of Template object
$t = new KTemplate("template.tpl");
// assign a string variable to a placeholder (simple replacing)
$t->content = "Testing templates...";
// remove the block "temptest" from main template and replace it
// with appropriate placeholder
$t_temptest = $t->temptest;
// assign a string to placeholder {item} (which belongs to the
// new Template object $t_temptest
$t_temptest->item = "first entry";
// assign the Template object $t_temptest to his previously
// inserted placeholder of the main template
$t->temptest = $t_temptest;
// reset (i.e. delete) contents of object $t_template,
// so its placeholder can be filled with new values.
// If you don't clear it, the contents will be added.
$t_temptest->reset();
// same as before - assign a string to the {item} placeholder
$t_temptest->item = "second entry";
// ...and put the object into its placeholder
$t->temptest = $t_temptest;
// now fetch the three nested blocks into three Template object instances
// Note: The block "table" isn't necessary in every case, but I want
// to demonstrate that it is possible to re-use complete blocks.
$t_table = $t->table;
$t_row = $t_table->row;
$t_col = $t_row->col;
// build a table with 5 rows and 5 columns
for ($row = 1; $row <= 5; $row++)
{
for ($col = 1; $col <= 5; $col++)
{
// assign a string to the column placeholder object instance
$t_col->cell_content = "foo $row $col";
// put column object instance into row object
$t_row->col = $t_col;
// clear content of column
$t_col->reset();
}
// put row into table
$t_table->row = $t_row;
// clear row content
$t_row->reset();
}
// put the complete table object into the main template
$t->table = $t_table;
// put the whole table (a Template object) into another placeholer
$t->my_content = $t_table;
// print the results
$t->out();
?>
Trackbacks
Keine Trackbacks vorhanden.




