|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
VEG Smarty Rookie
Joined: 02 May 2010 Posts: 20 Location: Belarus
|
Posted: Sat Jul 10, 2010 5:17 pm Post subject: Class wrapper for block plugins |
|
|
This wrapper solves the problem of sub-blocks of one type.
Code: | class SampleBlock
{
private $params;
private $smarty;
private $template;
private $counter = 3; // each block has its own variable
function __construct($params, $smarty, $template)
{
$this->params = $params;
$this->smarty = $smarty;
$this->template = $template;
}
function __destruct()
{
}
function enter()
{
extract($this->params);
}
// return true for repeat current block
function leave(&$content)
{
$this->counter--;
return ($this->counter > 0);
}
}
/**
* @param string $content contents of the block
* @param object $smarty Smarty object
* @param boolean $ &$repeat repeat flag
* @param object $template template object
* @return string content re-formatted
*/
function smarty_block_test($params, $content, $smarty, &$repeat, $template)
{
static $objs = array();
if(is_null($content))
{
$objs[] = $block = new SampleBlock($params, $smarty, $template);
$block->enter();
}
else
{
$block = end($objs);
if($block->leave($content))
{
$repeat = true;
}
else
{
$block = array_pop($objs);
unset($block);
}
return $content;
}
} |
Template {test}1{test}2{/test}3{/test} outputs 122231222312223
Sorry for my English. |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Sat Jul 10, 2010 8:20 pm Post subject: |
|
|
Why creating a class ffor it? A stack for the params and whatever flags controling the status of repeat which smarty_block_test would do same job. |
|
Back to top |
|
VEG Smarty Rookie
Joined: 02 May 2010 Posts: 20 Location: Belarus
|
Posted: Thu Jul 15, 2010 12:45 pm Post subject: |
|
|
U.Tews wrote: | Why creating a class ffor it? A stack for the params and whatever flags controling the status of repeat which smarty_block_test would do same job. | Because it is inconvenient. PHP mess templates also do the same thing, but I use Smarty.
Sorry for my English. |
|
Back to top |
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|