3 * Smarty Internal Plugin Templateparser Parse Tree
4 * These are classes to build parse tree in the template parser
8 * @author Thue Kristensen
16 * @subpackage Compiler
19 class Smarty_Internal_ParseTree_Template extends Smarty_Internal_ParseTree
23 * Array of template elements
27 public $subtrees = Array();
30 * Create root of parse tree for template elements
33 public function __construct()
38 * Append buffer to subtree
40 * @param \Smarty_Internal_Templateparser $parser
41 * @param Smarty_Internal_ParseTree $subtree
43 public function append_subtree(Smarty_Internal_Templateparser $parser, Smarty_Internal_ParseTree $subtree)
45 if (!empty($subtree->subtrees)) {
46 $this->subtrees = array_merge($this->subtrees, $subtree->subtrees);
48 if ($subtree->data !== '') {
49 $this->subtrees[] = $subtree;
55 * Append array to subtree
57 * @param \Smarty_Internal_Templateparser $parser
58 * @param \Smarty_Internal_ParseTree[] $array
60 public function append_array(Smarty_Internal_Templateparser $parser, $array = array())
63 $this->subtrees = array_merge($this->subtrees, (array) $array);
68 * Prepend array to subtree
70 * @param \Smarty_Internal_Templateparser $parser
71 * @param \Smarty_Internal_ParseTree[] $array
73 public function prepend_array(Smarty_Internal_Templateparser $parser, $array = array())
76 $this->subtrees = array_merge((array) $array, $this->subtrees);
81 * Sanitize and merge subtree buffers together
83 * @param \Smarty_Internal_Templateparser $parser
85 * @return string template code content
87 public function to_smarty_php(Smarty_Internal_Templateparser $parser)
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 == '')) {
96 if ($this->subtrees[ $key ]->data == '') {
99 $subtree .= $this->subtrees[ $key ]->to_smarty_php($parser);
101 if ($subtree == '') {
104 $code .= preg_replace('/((<%)|(%>)|(<\?php)|(<\?)|(\?>)|(<\/?script))/', "<?php echo '\$1'; ?>\n",
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 == '')) {
113 if ($this->subtrees[ $key ]->data == '') {
116 $subtree = $parser->compiler->appendCode($subtree, $this->subtrees[ $key ]->to_smarty_php($parser));
118 if ($subtree == '') {
124 $code .= $this->subtrees[ $key ]->to_smarty_php($parser);