[HTML_Template_Sigma] Behaviour of global variables
| From: | Markus Ernst | Date: | Mon, 10 Mar 2008 16:15:06 +0000 |
| Subject: | [HTML_Template_Sigma] Behaviour of global variables | ||
| Groups: | php.pear.general | ||
| Request: | Send a blank email to pear-general+get-28625@lists.php.net to get a copy of this message | ||
Hello
The combination of features of global variables in Sigma - not being cleared by clearVariables(), and not making blocks not empty when substituted - makes sense in many cases, such as the one described in the manual.
Anyway, in some cases I would be happy to have a variable that survives until the end of the script, but actually does make a block not empty. I encountered that while working with block template files and caching:
if ($output = $cache->get('page_contents')) {
$tpl->setGlobalVariable('page_contents', $output);
}
else {
$tpl->addBlockfile('page_contents', 'contents_block', 'def_tpl.html');
$tpl->setCurrentBlock('contents_block');
$contents = myContentGatheringFunction();
foreach ($contents as $c) {
$tpl->replaceBlockfile('contents_block', $c['template'], true);
$tpl->setVariable($c);
$tpl->parseCurrentBlock();
$tpl->clearVariables();
}
$cache->save('page_contents', $tpl->get('contents_block'));
}
$tpl->setGlobalVariable('page_title', $myPageTitle);
And the page template says:
<h1>{page_title}</h1>
{page_contents}
This worked like a charm - until a template author decided to surround the page contents placeholder by some presentational box:
<h1>{page_title}</h1>
<div style="border:5px dotted red; background:yellow">
{page_contents}
</div>
In order to hide the box when it is empty, the template author would surround it by a block:
<h1>{page_title}</h1>
<!-- BEGIN contents_block -->
<div style="border:5px dotted red; background:yellow">
{page_contents}
</div>
<!-- END contents_block -->
Now, this works at the first call of the page, when no cache is available. After that, the contents will disappear, as the global variable does not make the surrounding block not empty.
I solved this for me with this workaround:
if ($output = $cache->get('page_contents')) {
$tpl->addBlock('page_contents', 'contents_block', '{contents_cache}');
$tpl->setVariable('contents_cache', $output);
$tpl->setCurrentBlock('contents_block');
$tpl->parseCurrentBlock();
$tpl->clearVariables();
}
else {
....
Another workaround would be to introduce a standard blockname for the parent block of the global variable's placeholder, which then can be touched and parsed. But both are quite complicated.
I actually see 2 possible improvements that could make handling this kind of things easier:
1. Add option to setGlobalVariable()
A new parameter: boolean $make_block_not_empty is added; if set to true, a possible containing block would be handled the same way as if the variable was not global:
if ($output = $cache->get('page_contents')) {
$tpl->setGlobalVariable('page_contents', $output, true);
}
2. Add method to detect a parent block of a placeholder
A new method getParentBlock(string $child) is introduced; $child is the name of the placeholder or block that is to be checked for a parent. It might be useful to return false if the parent is __global__:
if ($output = $cache->get('page_contents')) {
$tpl->setGlobalVariable('page_contents', $output);
if ($parent_block = $tpl->getParentBlock('page_contents')) {
$tpl->parse($parent_block);} } -- Markus