3 * This is the old template engine, now deprecated.
4 * Friendica's default template engine is Smarty3 (see include/friendica_smarty.php)
7 require_once 'object/TemplateEngine.php';
9 define("KEY_NOT_EXISTS", '^R_key_not_Exists^');
11 class Template implements ITemplateEngine {
12 static $name ="internal";
24 private function _preg_error() {
26 switch (preg_last_error()) {
27 case PREG_INTERNAL_ERROR: echo('PREG_INTERNAL_ERROR');
29 case PREG_BACKTRACK_LIMIT_ERROR: echo('PREG_BACKTRACK_LIMIT_ERROR');
31 case PREG_RECURSION_LIMIT_ERROR: echo('PREG_RECURSION_LIMIT_ERROR');
33 case PREG_BAD_UTF8_ERROR: echo('PREG_BAD_UTF8_ERROR');
35 // This is only valid for php > 5.3, not certain how to code around it for unit tests
36 // case PREG_BAD_UTF8_OFFSET_ERROR: echo('PREG_BAD_UTF8_OFFSET_ERROR'); break;
38 //die("Unknown preg error.");
42 debug_print_backtrace();
46 private function _push_stack() {
47 $this->stack[] = array($this->r, $this->nodes);
50 private function _pop_stack() {
51 list($this->r, $this->nodes) = array_pop($this->stack);
54 private function _get_var($name, $retNoKey = false) {
55 $keys = array_map('trim', explode(".", $name));
56 if ($retNoKey && !array_key_exists($keys[0], $this->r))
57 return KEY_NOT_EXISTS;
59 foreach ($keys as $k) {
60 $val = (isset($val[$k]) ? $val[$k] : null);
68 * {{ if <$var> }}...[{{ else }} ...] {{ endif }}
69 * {{ if <$var>==<val|$var> }}...[{{ else }} ...]{{ endif }}
70 * {{ if <$var>!=<val|$var> }}...[{{ else }} ...]{{ endif }}
72 private function _replcb_if($args) {
73 if (strpos($args[2], "==") > 0) {
74 list($a, $b) = array_map("trim", explode("==", $args[2]));
75 $a = $this->_get_var($a);
77 $b = $this->_get_var($b);
79 } else if (strpos($args[2], "!=") > 0) {
80 list($a, $b) = array_map("trim", explode("!=", $args[2]));
81 $a = $this->_get_var($a);
83 $b = $this->_get_var($b);
86 $val = $this->_get_var($args[2]);
88 $x = preg_split("|{{ *else *}}|", $args[3]);
89 return ( $val ? $x[0] : (isset($x[1]) ? $x[1] : ""));
95 * {{ for <$var> as $name }}...{{ endfor }}
96 * {{ for <$var> as $key=>$name }}...{{ endfor }}
98 private function _replcb_for($args) {
99 $m = array_map('trim', explode(" as ", $args[2]));
100 $x = explode("=>", $m[1]);
101 if (count($x) == 1) {
105 list($keyname, $varname) = $x;
107 if ($m[0] == "" || $varname == "" || is_null($varname))
108 die("template error: 'for " . $m[0] . " as " . $varname . "'");
109 //$vals = $this->r[$m[0]];
110 $vals = $this->_get_var($m[0]);
112 if (!is_array($vals))
114 foreach ($vals as $k => $v) {
115 $this->_push_stack();
119 $r[$keyname] = (($k === 0) ? '0' : $k);
120 $ret .= $this->replace($args[3], $r);
129 * {{ inc <templatefile> [with $var1=$var2] }}{{ endinc }}
131 private function _replcb_inc($args) {
132 if (strpos($args[2], "with")) {
133 list($tplfile, $newctx) = array_map('trim', explode("with", $args[2]));
135 $tplfile = trim($args[2]);
139 if ($tplfile[0] == "$")
140 $tplfile = $this->_get_var($tplfile);
142 $this->_push_stack();
144 if (!is_null($newctx)) {
145 list($a, $b) = array_map('trim', explode("=", $newctx));
146 $r[$a] = $this->_get_var($b);
148 $this->nodes = Array();
149 $tpl = get_markup_template($tplfile);
150 $ret = $this->replace($tpl, $r);
158 * {{ debug $var [$var [$var [...]]] }}{{ enddebug }}
160 * replace node with <pre>var_dump($var, $var, ...);</pre>
162 private function _replcb_debug($args) {
163 $vars = array_map('trim', explode(" ", $args[2]));
167 foreach ($vars as $var) {
168 $ret .= htmlspecialchars(var_export($this->_get_var($var), true));
175 private function _replcb_node($m) {
176 $node = $this->nodes[$m[1]];
177 if (method_exists($this, "_replcb_" . $node[1])) {
178 $s = call_user_func(array($this, "_replcb_" . $node[1]), $node);
182 $s = preg_replace_callback('/\|\|([0-9]+)\|\|/', array($this, "_replcb_node"), $s);
186 private function _replcb($m) {
187 //var_dump(array_map('htmlspecialchars', $m));
189 $this->nodes[] = (array) $m;
190 return "||" . (count($this->nodes) - 1) . "||";
193 private function _build_nodes($s) {
195 while (!$this->done) {
197 $s = preg_replace_callback('|{{ *([a-z]*) *([^}]*)}}([^{]*({{ *else *}}[^{]*)?){{ *end\1 *}}|', array($this, "_replcb"), $s);
199 $this->_preg_error();
201 //({{ *else *}}[^{]*)?
202 krsort($this->nodes);
206 private function var_replace($s) {
210 * (\[)? optional open square bracket
211 * ([a-zA-Z0-9-_]+\.?)+ var name, followed by optional
212 * dot, repeated at least 1 time
213 * (|[a-zA-Z0-9-_:]+)* pipe followed by filter name and args, zero or many
214 * (?(1)\]) if there was opened square bracket
215 * (subgrup 1), match close bracket
217 if (preg_match_all('/\$(\[)?([a-zA-Z0-9-_]+\.?)+(\|[a-zA-Z0-9-_:]+)*(?(1)\])/', $s, $m)) {
218 foreach ($m[0] as $var) {
220 $exp = str_replace(array("[", "]"), array("", ""), $var);
221 $exptks = explode("|", $exp);
225 $val = $this->_get_var($varn, true);
226 if ($val != KEY_NOT_EXISTS) {
229 * Filter are in form of:
230 * filtername:arg:arg:arg
232 * "filtername" is function name
233 * "arg"s are optional, var value is appended to the end
234 * if one "arg"==='x' , is replaced with var value
237 * $item.body|htmlspecialchars // escape html chars
238 * $item.body|htmlspecialchars|strtoupper // escape html and uppercase result
239 * $item.created|date:%Y %M %j // format date (created is a timestamp)
240 * $item.body|str_replace:cat:dog // replace all "cat" with "dog"
241 * $item.body|str_replace:cat:dog:x:1 // replace one "cat" with "dog"
244 foreach ($exptks as $filterstr) {
245 $filter = explode(":", $filterstr);
246 $filtername = $filter[0];
248 $valkey = array_search("x", $filter);
249 if ($valkey === false) {
252 $filter[$valkey] = $val;
254 if (function_exists($filtername)) {
255 $val = call_user_func_array($filtername, $filter);
258 $s = str_replace($var, $val, $s);
266 // TemplateEngine interface
267 public function replace_macros($s, $r) {
270 // remove comments block
271 $s = preg_replace('/{#(.*?\s*?)*?#}/', "", $s);
273 $s = $this->_build_nodes($s);
275 $s = preg_replace_callback('/\|\|([0-9]+)\|\|/', array($this, "_replcb_node"), $s);
277 $this->_preg_error();
279 // replace strings recursively (limit to 10 loops)
282 while ($os != $s && $count < 10) {
285 $s = $this->var_replace($s);
287 return template_unescape($s);
290 public function get_template_file($file, $root='') {
292 $template_file = get_template_file($a, $file, $root);
293 $content = file_get_contents($template_file);
300 function template_escape($s) {
302 return str_replace(array('$', '{{'), array('!_Doll^Ars1Az_!', '!_DoubLe^BraceS4Rw_!'), $s);
305 function template_unescape($s) {
307 return str_replace(array('!_Doll^Ars1Az_!', '!_DoubLe^BraceS4Rw_!'), array('$', '{{'), $s);