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