class COBTemplate
Important note: This library uses features of PHP language which
have appeared in version 4.0.5! Please, update your PHP instalation.
Constructor: COBTemplate($tmplFile [, $isDynamic=false])
Instantiates an object and loads a template from local file tmplFile.
An optional boolean parameter isDynamic shows whether the template we
are loading is dynamic. i.e. We use dynamic templates to create an HTML table having its
single row templated.
Method: data([$blockName])
After calling this method with a parameter, all output done by PHP (including
HTML tags outside of PHP code, output of functions echo() and
print(), etc.) will replace the block {blockName}
in a temlpate.
Call this method without parameters to stop collecting output for block's value.
Also, the collecting stops when you call update() or finish()
methods of the same object.
Method: set($blockName [, $blockValue])
There are multiple forms of calling this method:
when you pass only a single variable to this method, the block {blockName}
will be replaced with an empty string
when you call this method passing an array as a single parameter, all blocks named
by array keys will be replaced with their respective array
values
when the object is dynamic and you pass single parameter as a nested array,
all blocks will take their values from arrays which are values of blockName
array. So, you will have sizeof($blockName) iterations of a dynamic block
if called with two arguments and both of them are variables, the block
{blockName} will be replaced with blockValue
if called with two arguments and blockName is an array, the
blockValue will be put instead of all blocks named by
array values
when blockValue is an instance of the COBTemplate class, the block
{blockName} will be changed to the blockValue's content
Method: update()
Used with dynamic templates. This method should be called after filling a dynamic template
to pass to the next iteration.
Method: finish($useGarbage=0)
Finishes currently opened block (if there is one) and terminates work with
template sending it to the browser.
Also, if user's browser accepts gzip transfer encoding, the content will be
gzip'ed which makes transfer much faster.
Optional parameter useGarbage tells what to do with all
data which does not belong to any block:
-1 : send before template
0 : just ignore
1 : send after template
Sample template: test.html
<html><head>
<title>{title}</title>
</head>
<body>
<center><h1>{welcome}</h1></center>
<br>
<table align=center border=3 width=50%>
{rows}
</table>
</body></html>
Sample template: testrow.html
<tr>
<td align=left width=33%>{a}</td>
<td align=center width=33%>{b}</td>
<td align=right width=33%>{c}</td>
</tr>
Sample code: test.php
<?php
require_once('obtemplate.php');
$Template = new COBTemplate('test.html');
$Template->set('title', 'This is the title');
$Template->data('welcome');
echo('You just echo() data and it will be placed instead of a block');
$Template->data();
$Row = new COBTemplate('testrow.html', true);
for($i=1; $i<=3; ++) {
$a[] = array(
'a' => $i*2,
'b' => $i*3,
'c' => $i*4
);
}
$Row->set($a);
$Row->set(array('a'=>100, 'b'=>200, 'c'=>300));
$Template->set('rows', &$Row);
$Template->finish();
?>
|