blob: 0dbd9463d0ca8ff308b8077efed076c81018ecc1 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
|
<?php
/*
* Node.php - template parser node interface and classes
*
* Copyright (c) 2013 Elmar Ludwig
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*/
namespace exTpl;
require_once 'Context.php';
require_once 'Expression.php';
/**
* Basic interface for nodes in the template parse tree. The only
* required method is "render" to render a node and its children.
*/
interface Node
{
/**
* Returns a string representation of this node.
*
* @param Context $context symbol table
*/
public function render($context);
}
/**
* TextNode represents a verbatim text segment.
*/
class TextNode implements Node
{
protected $text;
/**
* Initializes a new Node instance with the given text.
*
* @param string $text verbatim text
*/
public function __construct($text)
{
$this->text = $text;
}
/**
* Returns a string representation of this node.
*
* @param Context $context symbol table
*/
public function render($context)
{
return $this->text;
}
}
/**
* ExpressionNode represents an expression tag: "{...}".
*/
class ExpressionNode implements Node
{
protected $expr;
/**
* Initializes a new Node instance with the given expression.
*
* @param Expression $expr expression object
*/
public function __construct(Expression $expr)
{
$this->expr = $expr;
}
/**
* Returns a string representation of this node.
*
* @param Context $context symbol table
*/
public function render($context)
{
$value = $this->expr->value($context);
if (!($this->expr instanceof RawExpression)) {
$value = $context->escape($value);
}
return $value;
}
}
/**
* ArrayNode represents a sequence of arbitrary nodes.
*/
class ArrayNode implements Node
{
protected $nodes = array();
/**
* Adds a child node to this sequence node.
*
* @param Node $node child node to add
*/
public function addChild(Node $node)
{
$this->nodes[] = $node;
}
/**
* Returns a string representation of this node.
*
* @param Context $context symbol table
*/
public function render($context)
{
$result = '';
foreach ($this->nodes as $node) {
$result .= $node->render($context);
}
return $result;
}
}
/**
* IteratorNode represents a single iterator tag:
* "{foreach ARRAY [as [KEY =>] VALUE]}...{endforeach}".
*/
class IteratorNode extends ArrayNode
{
protected $expr;
protected $key_name;
protected $val_name;
/**
* Initializes a new Node instance with the given expression.
*
* @param Expression $expr expression object
* @param string $key_name name of variable on each iteration
* @param string $val_name name of variable on each iteration
*/
public function __construct(Expression $expr, $key_name, $val_name)
{
$this->expr = $expr;
$this->key_name = $key_name;
$this->val_name = $val_name;
}
/**
* Returns a string representation of this node. The IteratorNode
* renders the node sequence for each value in the expression list.
*
* @param Context $context symbol table
*/
public function render($context)
{
$values = $this->expr->value($context);
$result = '';
if (is_array($values) && is_int(key($values))) {
$bindings = array($this->key_name => &$key, $this->val_name => &$value);
$context = new Context($bindings, $context);
foreach ($values as $key => $value) {
$result .= parent::render(new Context($value, $context));
}
} else if (is_array($values) && count($values)) {
return parent::render(new Context($values, $context));
} else if ($values) {
return parent::render($context);
}
return $result;
}
}
/**
* ConditionNode represents a single condition tag:
* "{if CONDITION}...{else}...{endif}".
*/
class ConditionNode extends ArrayNode
{
protected $condition;
protected $else_node;
/**
* Initializes a new Node instance with the given expression.
*
* @param Expression $condition expression object
*/
public function __construct(Expression $condition)
{
$this->condition = $condition;
}
/**
* Adds an else block to this condition node.
*/
public function addElse()
{
$this->else_node = new ArrayNode();
}
/**
* Adds a child node to this condition node.
*
* @param Node $node child node to add
*/
public function addChild(Node $node)
{
if ($this->else_node) {
$this->else_node->addChild($node);
} else {
parent::addChild($node);
}
}
/**
* Returns a string representation of this node.
*
* @param Context $context symbol table
*/
public function render($context)
{
if ($this->condition->value($context)) {
return parent::render($context);
}
return $this->else_node ? $this->else_node->render($context) : '';
}
}
|