For Programmers: Free Programming Magazines  


Home > Archive > PHP Pear > March 2007 > Html_Template_Sigma: Set fallback for stripped empty blocks at template









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Html_Template_Sigma: Set fallback for stripped empty blocks at template
Markus Ernst

2007-03-15, 7:05 pm

Hello

I try to find some kind of fallback scenario for empty blocks that could
be applied in the template (without touching the variable setting
engine). The empty blocks stripping feature is very handy for keeping
the output clean without the need of explicitly parsing blocks, such as:

<!-- BEGIN person_block -->
<h2>{name}</h2>
<!-- BEGIN person_image_block -->
<p><img src="{img-src}"></p>
<!-- END person_image_block -->
[...]
<!-- END person_block -->

As the application that sets the variables does not need to know about
person_image_block, it just sets the img-src variable if the image is
available.

Now I would like to extend this to some kind of fallback scenario, let's
say the person record can contain a video and/or a portrait image, or
none of both. I would like to show the Video, if it is not available the
portrait image, if both are not available a default "coming soon" image.
To make myself understandable - without a template engine I would write
this code:

if (isset($person['video-src'])) {
echo '<p><object [...] data="'.$person['video-src'].'" [...]></p>';
}
else if (isset($person['img-src'])) {
echo '<p><img src="'.$person['img-src'].'"></p>';
}
else {
echo '<p><img src="/pix/coming-soon.gif"></p>';
}

Is it possible to realize something like this at template level? A
general modification at application level is possible, for example
setting a callback function; but I am looking for a solution that is
independent from the variable setting process, as the application does
not know which data are possibly to be replaced by which else. It just
knows:
$tpl->setVariable($person);
$tpl->setCurrentBlock('person_block');
$tpl->parseCurrentBlock();

And I imagine a template somehow like:

<!-- BEGIN person_block -->
<h2>{name}</h2>
<!-- BEGIN portrait_block -->
<!-- BEGIN portrait_subblock_1 -->
<p><img src="{video-src}"></p>
<!-- END portrait_subblock_1 -->
<!-- BEGIN portrait_subblock_2 -->
<p><img src="{img-src}"></p>
<!-- END portrait_subblock_2 -->
<!-- BEGIN portrait_subblock_3 -->
<p><img src="/pix/coming-soon.gif"></p>
<!-- END portrait_subblock_3 -->
<!-- END portrait_block -->
<!-- END person_block -->

And at parsing time all subblocks after the first non-empty one should
be removed, but if all subblocks are empty, the last one should be
touched...

Thanks for any ideas, and also for a comment in case there is no
possible solution!

--
Markus
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com