]> git.mxchange.org Git - friendica.git/blob - include/template_processor.php
Add {{ if a==b }} and {{ if a!=b }} to templates
[friendica.git] / include / template_processor.php
1 <?php
2
3         class Template {
4                 var $r;
5                 var $search;
6                 var $replace;
7                 var $stack = array();
8                 var $nodes = array();
9                 var $done = false;
10                 
11                 private function _build_replace($r, $prefix){
12         
13                         if(is_array($r) && count($r)) {
14                                 foreach ($r as $k => $v ) {
15                                         if (is_array($v))
16                                                 $this->_build_replace($v, "$prefix$k.");
17                                         
18                                         $this->search[] =  $prefix . $k;
19                                         $this->replace[] = $v;
20                                 }
21                         }
22                 } 
23                 
24                 private function _push_stack(){
25                         $this->stack[] = array($this->r, $this->search, $this->replace, $this->nodes);
26                 }
27                 private function _pop_stack(){
28                         list($this->r, $this->search, $this->replace, $this->nodes) = array_pop($this->stack);
29                 }
30                 
31                 private function _get_var($name){
32                         $keys = array_map('trim',explode(".",$name));                   
33                         $val = $this->r;
34                         foreach($keys as $k) {
35                                 $val = $val[$k];
36                         }
37                         return $val;
38                 }
39                 
40                 /**
41                  * IF node
42                  * 
43                  * {{ if <$var> }}...{{ endif }}
44                  * {{ if <$var>==<val|$var> }}...{{ endif }}
45                  * {{ if <$var>!=<val|$var> }}...{{ endif }}
46                  */
47                 private function _replcb_if($args){
48                         
49                         if (strpos($args[2],"==")>0){
50                                 list($a,$b) = array_map("trim",explode("==",$args[2]));
51                                 $a = $this->_get_var($a);
52                                 if ($b[0]=="$") $b =  $this->_get_var($b);
53                                 $val = ($a == $b);
54                         } else if (strpos($args[2],"!=")>0){
55                                 list($a,$b) = explode("!=",$args[2]);
56                                 $a = $this->_get_var($a);
57                                 if ($b[0]=="$") $b =  $this->_get_var($b);
58                                 $val = ($a != $b);
59                         } else {
60                                 $val = $this->_get_var($args[2]);
61                         }
62                         return ($val?$args[3]:"");
63                 }
64                 
65                 /**
66                  * FOR node
67                  * 
68                  * {{ for <$var> as $name }}...{{ endfor }}
69                  * {{ for <$var> as $key=>$name }}...{{ endfor }}
70                  */
71                 private function _replcb_for($args){
72                         $m = array_map('trim', explode(" as ", $args[2]));
73                         list($keyname, $varname) = explode("=>",$m[1]);
74                         if (is_null($varname)) { $varname=$keyname; $keyname=""; }
75                         if ($m[0]=="" || $varname=="" || is_null($varname)) die("template error: 'for ".$m[0]." as ".$varname."'") ;
76                         //$vals = $this->r[$m[0]];
77                         $vals = $this->_get_var($m[0]);
78                         $ret="";
79                         if (!is_array($vals)) return $ret; 
80                         foreach ($vals as $k=>$v){
81                                 $this->_push_stack();
82                                 $r = $this->r;
83                                 $r[$varname] = $v;
84                                 if ($keyname!='') $r[$keyname] = $k;
85                                 $ret .=  $this->replace($args[3], $r);
86                                 $this->_pop_stack();
87                         }
88                         return $ret;
89                 }
90
91                 /**
92                  * INC node
93                  * 
94                  * {{ inc <templatefile> [with $var1=$var2] }}{{ endinc }}
95                  */
96                 private function _replcb_inc($args){
97                         list($tplfile, $newctx) = array_map('trim', explode("with",$args[2]));
98                         $this->_push_stack();
99                         $r = $this->r;
100                         if (!is_null($newctx)) {
101                                 list($a,$b) = array_map('trim', explode("=",$newctx));
102                                 $r[$a] = $this->_get_var($b); 
103                         }
104                         $this->nodes = Array();
105                         $tpl = get_markup_template($tplfile);
106                         $ret = $this->replace($tpl, $r);
107                         $this->_pop_stack();
108                         return $ret;
109                         
110                 }
111
112                 private function _replcb_node($m) {
113                         $node = $this->nodes[$m[1]];
114                         if (method_exists($this, "_replcb_".$node[1])){
115                                 return call_user_func(array($this, "_replcb_".$node[1]),  $node);
116                         } else {
117                                 return "";
118                         }
119                 }
120                                                 
121                 private function _replcb($m){
122                         $this->done = false;    
123                         $this->nodes[] = (array) $m;
124                         return "||". (count($this->nodes)-1) ."||";
125                 }
126                 
127                 private function _build_nodes($s){
128                         $this->done = false;
129                         while (!$this->done){
130                                 $this->done=true;
131                                 $s = preg_replace_callback('|{{ *([a-z]*) *([^}]*)}}([^{]*){{ *end\1 *}}|', array($this, "_replcb"), $s);
132                         }
133                         krsort($this->nodes);
134                         return $s;
135                 }
136                 
137                 public function replace($s, $r) {
138                         $this->r = $r;
139                         $this->search = array();
140                         $this->replace = array();
141         
142                         $this->_build_replace($r, "");
143                         
144                         #$s = str_replace(array("\n","\r"),array("§n§","§r§"),$s);
145                         $s = $this->_build_nodes($s);
146                         $s = preg_replace_callback('/\|\|([0-9]+)\|\|/', array($this, "_replcb_node"), $s);
147                         $s = str_replace($this->search,$this->replace, $s);
148                         
149                         return $s;
150                 }
151         }
152         
153         $t = new Template;