3  * Smarty Internal Plugin Templateparser Parsetrees
 
   5  * These are classes to build parsetrees in the template parser
 
   9  * @author Thue Kristensen 
 
  13 abstract class _smarty_parsetree {
 
  14   abstract public function to_smarty_php();
 
  18  * A complete smarty tag.
 
  20 class _smarty_tag extends _smarty_parsetree
 
  24     public $saved_block_nesting;
 
  25     function __construct($parser, $data)
 
  27         $this->parser = $parser;
 
  29         $this->saved_block_nesting = $parser->block_nesting_level;
 
  32     public function to_smarty_php()
 
  37     public function assign_to_var()
 
  39         $var = sprintf('$_tmp%d', ++$this->parser->prefix_number);
 
  40         $this->parser->compiler->prefix_code[] = sprintf('<?php ob_start();?>%s<?php %s=ob_get_clean();?>',
 
  47  * Code fragment inside a tag.
 
  49 class _smarty_code extends _smarty_parsetree {
 
  52     function __construct($parser, $data)
 
  54         $this->parser = $parser;
 
  58     public function to_smarty_php()
 
  60         return sprintf("(%s)", $this->data);
 
  65  * Double quoted string inside a tag.
 
  67 class _smarty_doublequoted extends _smarty_parsetree {
 
  69     public $subtrees = Array();
 
  70     function __construct($parser, _smarty_parsetree $subtree)
 
  72         $this->parser = $parser;
 
  73         $this->subtrees[] = $subtree;
 
  74         if ($subtree instanceof _smarty_tag) {
 
  75             $this->parser->block_nesting_level = count($this->parser->compiler->_tag_stack);
 
  79     function append_subtree(_smarty_parsetree $subtree)
 
  81         $last_subtree = count($this->subtrees)-1;
 
  82         if ($last_subtree >= 0 && $this->subtrees[$last_subtree] instanceof _smarty_tag && $this->subtrees[$last_subtree]->saved_block_nesting < $this->parser->block_nesting_level) {
 
  83             if ($subtree instanceof _smarty_code) {
 
  84                 $this->subtrees[$last_subtree]->data .= '<?php echo ' . $subtree->data . ';?>';
 
  85             } elseif ($subtree instanceof _smarty_dq_content) {
 
  86                 $this->subtrees[$last_subtree]->data .= '<?php echo "' . $subtree->data . '";?>';
 
  88                 $this->subtrees[$last_subtree]->data .= $subtree->data;
 
  91             $this->subtrees[] = $subtree;
 
  93         if ($subtree instanceof _smarty_tag) {
 
  94             $this->parser->block_nesting_level = count($this->parser->compiler->_tag_stack);
 
  98     public function to_smarty_php()
 
 101         foreach ($this->subtrees as $subtree) {
 
 105             if ($subtree instanceof _smarty_tag) {
 
 106                 $more_php = $subtree->assign_to_var();
 
 108                 $more_php = $subtree->to_smarty_php();
 
 113             if (!$subtree instanceof _smarty_dq_content) {
 
 114                 $this->parser->compiler->has_variable_string = true;
 
 122  * Raw chars as part of a double quoted string.
 
 124 class _smarty_dq_content extends _smarty_parsetree {
 
 126     function __construct($parser, $data)
 
 128         $this->parser = $parser;
 
 132     public function to_smarty_php()
 
 134         return '"' . $this->data . '"';
 
 141 class _smarty_template_buffer extends _smarty_parsetree {
 
 142     public $subtrees = Array();
 
 143     function __construct($parser)
 
 145         $this->parser = $parser;
 
 148     function append_subtree(_smarty_parsetree $subtree)
 
 150         $this->subtrees[] = $subtree;
 
 153     public function to_smarty_php()
 
 156         for ($key = 0, $cnt = count($this->subtrees); $key < $cnt; $key++) {
 
 157             if ($key + 2 < $cnt) {
 
 158                 if ($this->subtrees[$key] instanceof _smarty_linebreak && $this->subtrees[$key + 1] instanceof _smarty_tag && $this->subtrees[$key + 1]->data == '' && $this->subtrees[$key + 2] instanceof _smarty_linebreak) {
 
 162                 if (substr($this->subtrees[$key]->data, -1) == '<' && $this->subtrees[$key + 1]->data == '' && substr($this->subtrees[$key + 2]->data, -1) == '?') {
 
 167             if (substr($code, -1) == '<') {
 
 168                 $subtree = $this->subtrees[$key]->to_smarty_php();
 
 169                 if (substr($subtree, 0, 1) == '?') {
 
 170                     $code = substr($code, 0, strlen($code)-1) . '<<?php ?>?' . substr($subtree, 1);
 
 171                 } elseif ($this->parser->asp_tags && substr($subtree, 0, 1) == '%') {
 
 172                     $code = substr($code, 0, strlen($code)-1) . '<<?php ?>%' . substr($subtree, 1);
 
 178             if ($this->parser->asp_tags && substr($code, -1) == '%') {
 
 179                 $subtree = $this->subtrees[$key]->to_smarty_php();
 
 180                 if (substr($subtree, 0, 1) == '>') {
 
 181                     $code = substr($code, 0, strlen($code)-1) . '%<?php ?>>' . substr($subtree, 1);
 
 187             if (substr($code, -1) == '?') {
 
 188                 $subtree = $this->subtrees[$key]->to_smarty_php();
 
 189                 if (substr($subtree, 0, 1) == '>') {
 
 190                     $code = substr($code, 0, strlen($code)-1) . '?<?php ?>>' . substr($subtree, 1);
 
 196             $code .= $this->subtrees[$key]->to_smarty_php();
 
 205 class _smarty_text extends _smarty_parsetree {
 
 207     function __construct($parser, $data)
 
 209         $this->parser = $parser;
 
 213     public function to_smarty_php()
 
 220  * template linebreaks
 
 222 class _smarty_linebreak extends _smarty_parsetree {
 
 224     function __construct($parser, $data)
 
 226         $this->parser = $parser;
 
 230     public function to_smarty_php()