library/smarty/libs/sysplugins/smarty_internal_parsetree_template.php
changeset 46 f11c31f7fa3e
parent 45 a56e7f9a0463
child 47 03388ec805b4
equal deleted inserted replaced
45:a56e7f9a0463 46:f11c31f7fa3e
     1 <?php
       
     2 /**
       
     3  * Smarty Internal Plugin Templateparser Parse Tree
       
     4  * These are classes to build parse tree in the template parser
       
     5  *
       
     6  * @package    Smarty
       
     7  * @subpackage Compiler
       
     8  * @author     Thue Kristensen
       
     9  * @author     Uwe Tews
       
    10  */
       
    11 
       
    12 /**
       
    13  * Template element
       
    14  *
       
    15  * @package    Smarty
       
    16  * @subpackage Compiler
       
    17  * @ignore
       
    18  */
       
    19 class Smarty_Internal_ParseTree_Template extends Smarty_Internal_ParseTree {
       
    20 
       
    21     /**
       
    22      * Array of template elements
       
    23      *
       
    24      * @var array
       
    25      */
       
    26     public $subtrees = Array();
       
    27 
       
    28     /**
       
    29      * Create root of parse tree for template elements
       
    30      *
       
    31      * @param object $parser parse object
       
    32      */
       
    33     public function __construct($parser) {
       
    34         $this->parser = $parser;
       
    35     }
       
    36 
       
    37     /**
       
    38      * Append buffer to subtree
       
    39      *
       
    40      * @param Smarty_Internal_ParseTree $subtree
       
    41      */
       
    42     public function append_subtree(Smarty_Internal_ParseTree $subtree) {
       
    43         if (!empty($subtree->subtrees)) {
       
    44             $this->subtrees = array_merge($this->subtrees, $subtree->subtrees);
       
    45         } else {
       
    46             if ($subtree->data !== '') {
       
    47                 $this->subtrees[] = $subtree;
       
    48             }
       
    49         }
       
    50     }
       
    51 
       
    52     /**
       
    53      * Sanitize and merge subtree buffers together
       
    54      *
       
    55      * @return string template code content
       
    56      */
       
    57     public function to_smarty_php() {
       
    58         $code = '';
       
    59         for ($key = 0, $cnt = count($this->subtrees); $key < $cnt; $key++) {
       
    60             if ($this->subtrees[$key] instanceof Smarty_Internal_ParseTree_Text) {
       
    61                 $subtree = $this->subtrees[$key]->to_smarty_php();
       
    62                 while ($key + 1 < $cnt && ($this->subtrees[$key + 1] instanceof Smarty_Internal_ParseTree_Text || $this->subtrees[$key + 1]->data == '')) {
       
    63                     $key++;
       
    64                     if ($this->subtrees[$key]->data == '') {
       
    65                         continue;
       
    66                     }
       
    67                     $subtree .= $this->subtrees[$key]->to_smarty_php();
       
    68                 }
       
    69                 if ($subtree == '') {
       
    70                     continue;
       
    71                 }
       
    72                 $code .= preg_replace('/((<%)|(%>)|(<\?php)|(<\?)|(\?>)|(<\/?script))/', "<?php echo '\$1'; ?>\n", $subtree);
       
    73                 continue;
       
    74             }
       
    75             if ($this->subtrees[$key] instanceof Smarty_Internal_ParseTree_Tag) {
       
    76                 $subtree = $this->subtrees[$key]->to_smarty_php();
       
    77                 while ($key + 1 < $cnt && ($this->subtrees[$key + 1] instanceof Smarty_Internal_ParseTree_Tag || $this->subtrees[$key + 1]->data == '')) {
       
    78                     $key++;
       
    79                     if ($this->subtrees[$key]->data == '') {
       
    80                         continue;
       
    81                     }
       
    82                     $subtree = $this->parser->compiler->appendCode($subtree, $this->subtrees[$key]->to_smarty_php());
       
    83                 }
       
    84                 if ($subtree == '') {
       
    85                     continue;
       
    86                 }
       
    87                 $code .= $subtree;
       
    88                 continue;
       
    89             }
       
    90             $code .= $this->subtrees[$key]->to_smarty_php();
       
    91         }
       
    92         return $code;
       
    93     }
       
    94 }