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)) {
115 foreach ($vals as $k => $v) {
116 $this->_push_stack();
119 if ($keyname != '') {
120 $r[$keyname] = (($k === 0) ? '0' : $k);
122 $ret .= $this->replace($args[3], $r);
131 * {{ inc <templatefile> [with $var1=$var2] }}{{ endinc }}
133 private function _replcb_inc($args) {
134 if (strpos($args[2], "with")) {
135 list($tplfile, $newctx) = array_map('trim', explode("with", $args[2]));
137 $tplfile = trim($args[2]);
141 if ($tplfile[0] == "$")
142 $tplfile = $this->_get_var($tplfile);
144 $this->_push_stack();
146 if (!is_null($newctx)) {
147 list($a, $b) = array_map('trim', explode("=", $newctx));
148 $r[$a] = $this->_get_var($b);
150 $this->nodes = Array();
151 $tpl = get_markup_template($tplfile);
152 $ret = $this->replace($tpl, $r);
160 * {{ debug $var [$var [$var [...]]] }}{{ enddebug }}
162 * replace node with <pre>var_dump($var, $var, ...);</pre>
164 private function _replcb_debug($args) {
165 $vars = array_map('trim', explode(" ", $args[2]));
169 foreach ($vars as $var) {
170 $ret .= htmlspecialchars(var_export($this->_get_var($var), true));
177 private function _replcb_node($m) {
178 $node = $this->nodes[$m[1]];
179 if (method_exists($this, "_replcb_" . $node[1])) {
180 $s = call_user_func(array($this, "_replcb_" . $node[1]), $node);
184 $s = preg_replace_callback('/\|\|([0-9]+)\|\|/', array($this, "_replcb_node"), $s);
188 private function _replcb($m) {
189 //var_dump(array_map('htmlspecialchars', $m));
191 $this->nodes[] = (array) $m;
192 return "||" . (count($this->nodes) - 1) . "||";
195 private function _build_nodes($s) {
197 while (!$this->done) {
199 $s = preg_replace_callback('|{{ *([a-z]*) *([^}]*)}}([^{]*({{ *else *}}[^{]*)?){{ *end\1 *}}|', array($this, "_replcb"), $s);
201 $this->_preg_error();
203 //({{ *else *}}[^{]*)?
204 krsort($this->nodes);
208 private function var_replace($s) {
212 * (\[)? optional open square bracket
213 * ([a-zA-Z0-9-_]+\.?)+ var name, followed by optional
214 * dot, repeated at least 1 time
215 * (|[a-zA-Z0-9-_:]+)* pipe followed by filter name and args, zero or many
216 * (?(1)\]) if there was opened square bracket
217 * (subgrup 1), match close bracket
219 if (preg_match_all('/\$(\[)?([a-zA-Z0-9-_]+\.?)+(\|[a-zA-Z0-9-_:]+)*(?(1)\])/', $s, $m)) {
220 foreach ($m[0] as $var) {
222 $exp = str_replace(array("[", "]"), array("", ""), $var);
223 $exptks = explode("|", $exp);
227 $val = $this->_get_var($varn, true);
228 if ($val != KEY_NOT_EXISTS) {
231 * Filter are in form of:
232 * filtername:arg:arg:arg
234 * "filtername" is function name
235 * "arg"s are optional, var value is appended to the end
236 * if one "arg"==='x' , is replaced with var value
239 * $item.body|htmlspecialchars // escape html chars
240 * $item.body|htmlspecialchars|strtoupper // escape html and uppercase result
241 * $item.created|date:%Y %M %j // format date (created is a timestamp)
242 * $item.body|str_replace:cat:dog // replace all "cat" with "dog"
243 * $item.body|str_replace:cat:dog:x:1 // replace one "cat" with "dog"
246 foreach ($exptks as $filterstr) {
247 $filter = explode(":", $filterstr);
248 $filtername = $filter[0];
250 $valkey = array_search("x", $filter);
251 if ($valkey === false) {
254 $filter[$valkey] = $val;
256 if (function_exists($filtername)) {
257 $val = call_user_func_array($filtername, $filter);
260 $s = str_replace($var, $val, $s);
268 // TemplateEngine interface
269 public function replace_macros($s, $r) {
272 // remove comments block
273 $s = preg_replace('/{#(.*?\s*?)*?#}/', "", $s);
275 $s = $this->_build_nodes($s);
277 $s = preg_replace_callback('/\|\|([0-9]+)\|\|/', array($this, "_replcb_node"), $s);
279 $this->_preg_error();
281 // replace strings recursively (limit to 10 loops)
284 while ($os != $s && $count < 10) {
287 $s = $this->var_replace($s);
289 return template_unescape($s);
292 public function get_template_file($file, $root='') {
294 $template_file = get_template_file($a, $file, $root);
295 $content = file_get_contents($template_file);
302 function template_escape($s) {
304 return str_replace(array('$', '{{'), array('!_Doll^Ars1Az_!', '!_DoubLe^BraceS4Rw_!'), $s);
307 function template_unescape($s) {
309 return str_replace(array('!_Doll^Ars1Az_!', '!_DoubLe^BraceS4Rw_!'), array('$', '{{'), $s);