]> git.mxchange.org Git - friendica.git/blob - include/template_processor.php
arrays and simple conditional blocks in template, template for nav, load templates...
[friendica.git] / include / template_processor.php
1 <?php
2         
3
4         class Template {
5                 var $s;
6                 var $r;
7                 var $search;
8                 var $replace;
9                 
10                 private function _build_replace($r, $prefix){
11         
12                         if(is_array($r) && count($r)) {
13                                 foreach ($r as $k => $v ) {
14                                         if (is_array($v))
15                                                 $this->_build_replace($v, "$prefix$k.");
16                                         
17                                         $this->search[] =  $prefix . $k;
18                                         $this->replace[] = $v;
19                                 }
20                         }
21                 } 
22                 
23                 private function _replcb_if($m){
24                         //echo "<pre>"; var_dump($m);
25                         $keys = explode(".",$m[1]);
26                         $val = $this->r;
27                         foreach($keys as $k) {
28                                 $val = $val[$k];
29                         }
30                         
31                         //echo $val;
32                         return ($val?$m[2]:"");
33                 }
34                 
35                 public function replace($s, $r) {
36                         $this->s = $s;
37                         $this->r = $r;
38                         $this->search = array();
39                         $this->replace = array();
40         
41                         $this->_build_replace($r, "");
42         
43                         
44                         $s = preg_replace_callback("|{{ *if *([^ }]*) *}}([^{]*){{ *endif *}}|", array($this, "_replcb_if"), $s);
45                         
46                         return str_replace($this->search,$this->replace,$s);
47                 }
48                 
49         }       
50         $t = new Template;