HTML_Template_Sigma: Feature suggestion - treating blocks as namespaces
| From: | Markus Ernst | Date: | Fri, 30 Dec 2005 11:57:30 +0000 |
| Subject: | HTML_Template_Sigma: Feature suggestion - treating blocks as namespaces | ||
| Groups: | php.pear.general | ||
| Request: | Send a blank email to pear-general+get-22389@lists.php.net to get a copy of this message | ||
Working with the Sigma template engine I encountered that sometimes it is useful to have the same variable names in different blocks of a template. It is possible to handle this by using global variables, clearing variables after parsing a block, and setting the variables in proper order.
Anyway it would be very handy to have an option of treating blocks as namespaces, thus preserve variables already set in other blocks. Example:
$tpl->setOption('treat_blocks_as_namespaces', true);
$tpl->setCurrentBlock('me');
$tpl->setVariable('name', 'Markus');
$tpl->setCurrentBlock('girlfriend');
$tpl->setVariable('name', 'Ina');
$tpl->parseCurrentBlock();
// Now variable 'name' in block 'me' would still be set
This is an easy example, and as mentioned I know how to work around it. But specially when blocks are nested such an option could make processing quite easy, as shown in this example:
// Template:
<!-- BEGIN category_level_1 -->
<h2>{category_title}</h2>
<p>{category_text}</p>
<ul>
<!-- BEGIN products -->
<li>{product_name}</li>
<!-- END products -->
</ul>
<!-- BEGIN category_level_2 -->
<h3>{category_title}</h3>
<ul>
<!-- BEGIN products -->
<li>{product_name}</li>
<!-- END products -->
</ul>
<!-- END category_level_2 -->
<!-- END category_level_1 -->
// Processing function:
function process_category(&$tpl, $level, $data) {
// Fill actual block's head section
$block = 'category_level_'.$level;
$tpl->setCurrentBlock($block);
$tpl->setVariable('category_title', $data['category_title']);
$tpl->setVariable('category_text', $data['category_text']);
// Fill and parse actual block's products section
foreach ($data['products'] as $product_name) {
$tpl->setCurrentBlock('products');
$tpl->setVariable('product_name', $product_name);
$tpl->parseCurrentBlock();
}
// Fill and parse sub blocks
if (!empty($data['sub_categories'])) {
foreach ($data['subcategories'] as $subdata) {
process_category($tpl, $level + 1, $subdata);
}
}
// Parse actual block
$tpl->setCurrentBlock($block);
$tpl->parseCurrentBlock();
}
// Process
$tpl->setOption('treat_blocks_as_namespaces', true);
process_category($tpl, 1, $data);
--
Markus