]> git.mxchange.org Git - friendica.git/blob - include/template_processor.php
fix template's {{ for }} variable lookup
[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                  */
45                 private function _replcb_if($args){
46                         $val = $this->_get_var($args[2]);
47                         return ($val?$args[3]:"");
48                 }
49                 
50                 /**
51                  * FOR node
52                  * 
53                  * {{ for <$var> as $name }}...{{ endfor }}
54                  * {{ for <$var> as $key=>$name }}...{{ endfor }}
55                  */
56                 private function _replcb_for($args){
57                         $m = array_map('trim', explode(" as ", $args[2]));
58                         list($keyname, $varname) = explode("=>",$m[1]);
59                         if (is_null($varname)) { $varname=$keyname; $keyname=""; }
60                         if ($m[0]=="" || $varname=="" || is_null($varname)) die("template error: 'for ".$m[0]." as ".$varname."'") ;
61                         //$vals = $this->r[$m[0]];
62                         $vals = $this->_get_var($m[0]);
63                         $ret="";
64                         if (!is_array($vals)) return $ret; 
65                         foreach ($vals as $k=>$v){
66                                 $this->_push_stack();
67                                 $r = $this->r;
68                                 $r[$varname] = $v;
69                                 if ($keyname!='') $r[$keyname] = $k;
70                                 $ret .=  $this->replace($args[3], $r);
71                                 $this->_pop_stack();
72                         }
73                         return $ret;
74                 }
75
76                 /**
77                  * INC node
78                  * 
79                  * {{ inc <templatefile> [with $var1=$var2] }}{{ endinc }}
80                  */
81                 private function _replcb_inc($args){
82                         list($tplfile, $newctx) = array_map('trim', explode("with",$args[2]));
83                         $this->_push_stack();
84                         $r = $this->r;
85                         if (!is_null($newctx)) {
86                                 list($a,$b) = array_map('trim', explode("=",$newctx));
87                                 $r[$a] = $this->_get_var($b); 
88                         }
89                         $this->nodes = Array();
90                         $tpl = get_markup_template($tplfile);
91                         $ret = $this->replace($tpl, $r);
92                         $this->_pop_stack();
93                         return $ret;
94                         
95                 }
96
97                 private function _replcb_node($m) {
98                         $node = $this->nodes[$m[1]];
99                         if (method_exists($this, "_replcb_".$node[1])){
100                                 return call_user_func(array($this, "_replcb_".$node[1]),  $node);
101                         } else {
102                                 return "";
103                         }
104                 }
105                                                 
106                 private function _replcb($m){
107                         $this->done = false;    
108                         $this->nodes[] = (array) $m;
109                         return "||". (count($this->nodes)-1) ."||";
110                 }
111                 
112                 private function _build_nodes($s){
113                         $this->done = false;
114                         while (!$this->done){
115                                 $this->done=true;
116                                 $s = preg_replace_callback('|{{ *([a-z]*) *([^}]*)}}([^{]*){{ *end\1 *}}|', array($this, "_replcb"), $s);
117                         }
118                         krsort($this->nodes);
119                         return $s;
120                 }
121                 
122                 public function replace($s, $r) {
123                         $this->r = $r;
124                         $this->search = array();
125                         $this->replace = array();
126         
127                         $this->_build_replace($r, "");
128                         
129                         #$s = str_replace(array("\n","\r"),array("§n§","§r§"),$s);
130                         $s = $this->_build_nodes($s);
131                         $s = preg_replace_callback('/\|\|([0-9]+)\|\|/', array($this, "_replcb_node"), $s);
132                         $s = str_replace($this->search,$this->replace, $s);
133                         
134                         return $s;
135                 }
136         }
137         
138         $t = new Template;