3 define("KEY_NOT_EXISTS", '^R_key_not_Exists^');
17 private function _preg_error() {
19 switch (preg_last_error()) {
20 case PREG_INTERNAL_ERROR: echo('PREG_INTERNAL_ERROR');
22 case PREG_BACKTRACK_LIMIT_ERROR: echo('PREG_BACKTRACK_LIMIT_ERROR');
24 case PREG_RECURSION_LIMIT_ERROR: echo('PREG_RECURSION_LIMIT_ERROR');
26 case PREG_BAD_UTF8_ERROR: echo('PREG_BAD_UTF8_ERROR');
28 // This is only valid for php > 5.3, not certain how to code around it for unit tests
29 // case PREG_BAD_UTF8_OFFSET_ERROR: echo('PREG_BAD_UTF8_OFFSET_ERROR'); break;
31 //die("Unknown preg error.");
35 debug_print_backtrace();
39 private function _push_stack() {
40 $this->stack[] = array($this->r, $this->nodes);
43 private function _pop_stack() {
44 list($this->r, $this->nodes) = array_pop($this->stack);
47 private function _get_var($name, $retNoKey = false) {
48 $keys = array_map('trim', explode(".", $name));
49 if ($retNoKey && !array_key_exists($keys[0], $this->r))
50 return KEY_NOT_EXISTS;
52 foreach ($keys as $k) {
53 $val = (isset($val[$k]) ? $val[$k] : null);
61 * {{ if <$var> }}...[{{ else }} ...] {{ endif }}
62 * {{ if <$var>==<val|$var> }}...[{{ else }} ...]{{ endif }}
63 * {{ if <$var>!=<val|$var> }}...[{{ else }} ...]{{ endif }}
65 private function _replcb_if($args) {
66 if (strpos($args[2], "==") > 0) {
67 list($a, $b) = array_map("trim", explode("==", $args[2]));
68 $a = $this->_get_var($a);
70 $b = $this->_get_var($b);
72 } else if (strpos($args[2], "!=") > 0) {
73 list($a, $b) = array_map("trim", explode("!=", $args[2]));
74 $a = $this->_get_var($a);
76 $b = $this->_get_var($b);
79 $val = $this->_get_var($args[2]);
81 $x = preg_split("|{{ *else *}}|", $args[3]);
82 return ( $val ? $x[0] : (isset($x[1]) ? $x[1] : ""));
88 * {{ for <$var> as $name }}...{{ endfor }}
89 * {{ for <$var> as $key=>$name }}...{{ endfor }}
91 private function _replcb_for($args) {
92 $m = array_map('trim', explode(" as ", $args[2]));
93 $x = explode("=>", $m[1]);
98 list($keyname, $varname) = $x;
100 if ($m[0] == "" || $varname == "" || is_null($varname))
101 die("template error: 'for " . $m[0] . " as " . $varname . "'");
102 //$vals = $this->r[$m[0]];
103 $vals = $this->_get_var($m[0]);
105 if (!is_array($vals))
107 foreach ($vals as $k => $v) {
108 $this->_push_stack();
112 $r[$keyname] = (($k === 0) ? '0' : $k);
113 $ret .= $this->replace($args[3], $r);
122 * {{ inc <templatefile> [with $var1=$var2] }}{{ endinc }}
124 private function _replcb_inc($args) {
125 if (strpos($args[2], "with")) {
126 list($tplfile, $newctx) = array_map('trim', explode("with", $args[2]));
128 $tplfile = trim($args[2]);
132 if ($tplfile[0] == "$")
133 $tplfile = $this->_get_var($tplfile);
135 $this->_push_stack();
137 if (!is_null($newctx)) {
138 list($a, $b) = array_map('trim', explode("=", $newctx));
139 $r[$a] = $this->_get_var($b);
141 $this->nodes = Array();
142 $tpl = get_markup_template($tplfile);
143 $ret = $this->replace($tpl, $r);
151 * {{ debug $var [$var [$var [...]]] }}{{ enddebug }}
153 * replace node with <pre>var_dump($var, $var, ...);</pre>
155 private function _replcb_debug($args) {
156 $vars = array_map('trim', explode(" ", $args[2]));
160 foreach ($vars as $var) {
161 $ret .= htmlspecialchars(var_export($this->_get_var($var), true));
168 private function _replcb_node($m) {
169 $node = $this->nodes[$m[1]];
170 if (method_exists($this, "_replcb_" . $node[1])) {
171 $s = call_user_func(array($this, "_replcb_" . $node[1]), $node);
175 $s = preg_replace_callback('/\|\|([0-9]+)\|\|/', array($this, "_replcb_node"), $s);
179 private function _replcb($m) {
180 //var_dump(array_map('htmlspecialchars', $m));
182 $this->nodes[] = (array) $m;
183 return "||" . (count($this->nodes) - 1) . "||";
186 private function _build_nodes($s) {
188 while (!$this->done) {
190 $s = preg_replace_callback('|{{ *([a-z]*) *([^}]*)}}([^{]*({{ *else *}}[^{]*)?){{ *end\1 *}}|', array($this, "_replcb"), $s);
192 $this->_preg_error();
194 //({{ *else *}}[^{]*)?
195 krsort($this->nodes);
199 private function var_replace($s) {
203 * (\[)? optional open square bracket
204 * ([a-zA-Z0-9-_]+\.?)+ var name, followed by optional
205 * dot, repeated at least 1 time
206 * (|[a-zA-Z0-9-_:]+)* pipe followed by filter name and args, zero or many
207 * (?(1)\]) if there was opened square bracket
208 * (subgrup 1), match close bracket
210 if (preg_match_all('/\$(\[)?([a-zA-Z0-9-_]+\.?)+(\|[a-zA-Z0-9-_:]+)*(?(1)\])/', $s, $m)) {
211 foreach ($m[0] as $var) {
213 $exp = str_replace(array("[", "]"), array("", ""), $var);
214 $exptks = explode("|", $exp);
218 $val = $this->_get_var($varn, true);
219 if ($val != KEY_NOT_EXISTS) {
222 * Filter are in form of:
223 * filtername:arg:arg:arg
225 * "filtername" is function name
226 * "arg"s are optional, var value is appended to the end
227 * if one "arg"==='x' , is replaced with var value
230 * $item.body|htmlspecialchars // escape html chars
231 * $item.body|htmlspecialchars|strtoupper // escape html and uppercase result
232 * $item.created|date:%Y %M %j // format date (created is a timestamp)
233 * $item.body|str_replace:cat:dog // replace all "cat" with "dog"
234 * $item.body|str_replace:cat:dog:x:1 // replace one "cat" with "dog"
237 foreach ($exptks as $filterstr) {
238 $filter = explode(":", $filterstr);
239 $filtername = $filter[0];
241 $valkey = array_search("x", $filter);
242 if ($valkey === false) {
245 $filter[$valkey] = $val;
247 if (function_exists($filtername)) {
248 $val = call_user_func_array($filtername, $filter);
251 $s = str_replace($var, $val, $s);
259 public function replace($s, $r) {
262 // remove comments block
263 $s = preg_replace('/{#(.*?\s*?)*?#}/', "", $s);
265 $s = $this->_build_nodes($s);
267 $s = preg_replace_callback('/\|\|([0-9]+)\|\|/', array($this, "_replcb_node"), $s);
269 $this->_preg_error();
271 // replace strings recursively (limit to 10 loops)
274 while ($os != $s && $count < 10) {
277 $s = $this->var_replace($s);
286 function template_escape($s) {
288 return str_replace(array('$', '{{'), array('!_Doll^Ars1Az_!', '!_DoubLe^BraceS4Rw_!'), $s);
291 function template_unescape($s) {
293 return str_replace(array('!_Doll^Ars1Az_!', '!_DoubLe^BraceS4Rw_!'), array('$', '{{'), $s);