Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Class wrapper for block plugins

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Smarty 3
View previous topic :: View next topic  
Author Message
VEG
Smarty Rookie


Joined: 02 May 2010
Posts: 20
Location: Belarus

PostPosted: Sat Jul 10, 2010 5:17 pm    Post subject: Class wrapper for block plugins Reply with quote

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
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Sat Jul 10, 2010 8:20 pm    Post subject: Reply with quote

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
View user's profile Send private message
VEG
Smarty Rookie


Joined: 02 May 2010
Posts: 20
Location: Belarus

PostPosted: Thu Jul 15, 2010 12:45 pm    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Smarty 3 All times are GMT
Page 1 of 1

 
Jump to:  
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
Protected by Anti-Spam ACP