Subversion Repositories ALCASAR

Rev

Details | Last modification | View Log

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