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 */ 19class Smarty_Internal_ParseTree_Template extends Smarty_Internal_ParseTree 20{ 21 22 /** 23 * Array of template elements 24 * 25 * @var array 26 */ 27 public $subtrees = Array(); 28 29 /** 30 * Create root of parse tree for template elements 31 * 32 */ 33 public function __construct() 34 { 35 } 36 37 /** 38 * Append buffer to subtree 39 * 40 * @param \Smarty_Internal_Templateparser $parser 41 * @param Smarty_Internal_ParseTree $subtree 42 */ 43 public function append_subtree(Smarty_Internal_Templateparser $parser, Smarty_Internal_ParseTree $subtree) 44 { 45 if (!empty($subtree->subtrees)) { 46 $this->subtrees = array_merge($this->subtrees, $subtree->subtrees); 47 } else { 48 if ($subtree->data !== '') { 49 $this->subtrees[] = $subtree; 50 } 51 } 52 } 53 54 /** 55 * Append array to subtree 56 * 57 * @param \Smarty_Internal_Templateparser $parser 58 * @param \Smarty_Internal_ParseTree[] $array 59 */ 60 public function append_array(Smarty_Internal_Templateparser $parser, $array = array()) 61 { 62 if (!empty($array)) { 63 $this->subtrees = array_merge($this->subtrees, (array) $array); 64 } 65 } 66 67 /** 68 * Prepend array to subtree 69 * 70 * @param \Smarty_Internal_Templateparser $parser 71 * @param \Smarty_Internal_ParseTree[] $array 72 */ 73 public function prepend_array(Smarty_Internal_Templateparser $parser, $array = array()) 74 { 75 if (!empty($array)) { 76 $this->subtrees = array_merge((array) $array, $this->subtrees); 77 } 78 } 79 80 /** 81 * Sanitize and merge subtree buffers together 82 * 83 * @param \Smarty_Internal_Templateparser $parser 84 * 85 * @return string template code content 86 */ 87 public function to_smarty_php(Smarty_Internal_Templateparser $parser) 88 { 89 $code = ''; 90 for ($key = 0, $cnt = count($this->subtrees); $key < $cnt; $key ++) { 91 if ($this->subtrees[ $key ] instanceof Smarty_Internal_ParseTree_Text) { 92 $subtree = $this->subtrees[ $key ]->to_smarty_php($parser); 93 while ($key + 1 < $cnt && ($this->subtrees[ $key + 1 ] instanceof Smarty_Internal_ParseTree_Text || 94 $this->subtrees[ $key + 1 ]->data == '')) { 95 $key ++; 96 if ($this->subtrees[ $key ]->data == '') { 97 continue; 98 } 99 $subtree .= $this->subtrees[ $key ]->to_smarty_php($parser); 100 } 101 if ($subtree == '') { 102 continue; 103 } 104 $code .= preg_replace('/((<%)|(%>)|(<\?php)|(<\?)|(\?>)|(<\/?script))/', "<?php echo '\$1'; ?>\n", 105 $subtree); 106 continue; 107 } 108 if ($this->subtrees[ $key ] instanceof Smarty_Internal_ParseTree_Tag) { 109 $subtree = $this->subtrees[ $key ]->to_smarty_php($parser); 110 while ($key + 1 < $cnt && ($this->subtrees[ $key + 1 ] instanceof Smarty_Internal_ParseTree_Tag || 111 $this->subtrees[ $key + 1 ]->data == '')) { 112 $key ++; 113 if ($this->subtrees[ $key ]->data == '') { 114 continue; 115 } 116 $subtree = $parser->compiler->appendCode($subtree, $this->subtrees[ $key ]->to_smarty_php($parser)); 117 } 118 if ($subtree == '') { 119 continue; 120 } 121 $code .= $subtree; 122 continue; 123 } 124 $code .= $this->subtrees[ $key ]->to_smarty_php($parser); 125 } 126 return $code; 127 } 128} 129