cvs: pear /HTML_Template_Sigma/docs example_1.php /HTML_Template_Sigma/docs/templates example_1.html
| From: | Alexey Borzov | Date: | Sun, 20 Apr 2003 12:40:32 +0000 |
| Subject: | cvs: pear /HTML_Template_Sigma/docs example_1.php /HTML_Template_Sigma/docs/templates example_1.html | ||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-9983@lists.php.net to get a copy of this message | ||
avb Sun Apr 20 08:40:32 2003 EDT
Added files:
/pear/HTML_Template_Sigma/docs/templates example_1.html
/pear/HTML_Template_Sigma/docs example_1.php
Log:
more usage examples
Index: pear/HTML_Template_Sigma/docs/templates/example_1.html
+++ pear/HTML_Template_Sigma/docs/templates/example_1.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Sigma example 1: basic variables and blocks</title>
</head>
<body>
<h1>Σ<sub>template</sub> example 1: basic variables and
blocks</h1>
<p>There are two types of variables in Sigma:</p>
<ul>
<li><i>local</i> variables: these get cleared after substitution and control
the empty/non-empty block logic</li>
<li><i>global</i> variables: these never get cleared and do not influence
block's empty status</li>
</ul>
<p>A block is considered empty if there are no local (to it) variables to substitute and the
block does not contain non-empty subblocks.</p>
<p>Unknown variable placeholders and empty blocks are removed by default. This can be toggled
in <b>loadTemplateFile()</b> call and there are means to manually control removal of
blocks (see example 2).</p>
<h2>1. Variable substitution</h2>
<p>Set via <b>setVariable()</b>: {var1}, {var2}, {var3}.</p>
<p>Set via <b>setGlobalVariable()</b>: {glob}.</p>
<p>Unknown placeholders to remove: {foo}, {bar}.</p>
<h2>2. Empty/nonempty blocks</h2>
<h3>2.1 Non-empty blocks</h3>
<hr>
<!-- BEGIN nonempty_1 -->
I am not empty, because a local variable is substituted: {var_ne_1}.
<!-- END nonempty_1 -->
<hr>
<!-- BEGIN nonempty_2 -->
I am not empty, because I have a non-empty subblock:
<!-- BEGIN subblock -->
(subblock: {var_ne_2})
<!-- END subblock -->
<!-- END nonempty_2 -->
<h3>2.2 Empty blocks</h3>
<!-- BEGIN empty_1 -->
I am empty, because here is no variable to substitute: {foo}.
<!-- END empty_1 -->
<hr>
<!-- BEGIN empty_2 -->
I am not empty, because I have a local variable: {var_e_2},<br>
<!-- BEGIN empty_subblock_2 -->
but subblock is, as he has no local variable of his own: {var_e_2}, {var_e_sub_2}.
<!-- END empty_subblock_2 -->
<!-- END empty_2 -->
<hr>
<!-- BEGIN empty_3 -->
I am empty, as I have only a global variable: {var_e_3}, {glob}
<!-- END empty_3 -->
<h2>3. Local/global difference</h2>
<p>Local variables are cleared after substitution, globals are not.</p>
<ul>
<!-- BEGIN list -->
<li>{local_1}, {local_2}, {glob}</li>
<!-- END list -->
</ul>
</body>
</html>
Index: pear/HTML_Template_Sigma/docs/example_1.php
+++ pear/HTML_Template_Sigma/docs/example_1.php
<?php
/**
* Example of usage for HTML_Template_Sigma, basic variables and blocks
*
* @package HTML_Template_Sigma
* @author Alexey Borzov <avb@php.net>
*
* $Id: example_1.php,v 1.1 2003/04/20 12:40:32 avb Exp $
*/
require_once 'HTML/Template/Sigma.php';
$listAry = array(
array('foo', 'bar'),
'stuff',
array('baz', 'quux'),
'more stuff'
);
// instantiate the template object, templates will be loaded from the
// 'templates' directory, no caching will take place
$tpl =& new HTML_Template_Sigma('./templates');
// No errors are expected to happen here
$tpl->setErrorHandling(PEAR_ERROR_DIE);
// default behaviour is to remove unknown variables and empty blocks
// from the template
$tpl->loadTemplateFile('example_1.html');
// 1. Variable substitution
// you can pass a name and a value to setVariable()
$tpl->setVariable('var1', 'Value 1');
// you can also pass an associative array
$tpl->setVariable(array(
'var2' => 'Value 2',
'var3' => 'Value 3'
));
// setGlobalVariable works the same
$tpl->setGlobalVariable('glob', 'I am global');
// 2. Empty/nonempty blocks
// 2.1 Non-empty blocks
$tpl->setVariable(array(
'var_ne_1' => 'Value for block 1',
'var_ne_2' => 'Value for subblock 2'
));
// 2.2 Empty blocks
$tpl->setVariable('var_e_2', 'Value for parent block');
// 3. Local/global difference
$tpl->setCurrentBlock('list');
foreach ($listAry as $item) {
if (is_array($item)) {
$tpl->setVariable(array(
'local_1' => $item[0],
'local_2' => $item[1]
));
} else {
$tpl->setVariable('local_1', $item);
}
$tpl->parseCurrentBlock();
}
// output the results
$tpl->show();
?>