aboutsummaryrefslogtreecommitdiffstats
path: root/include/template_processor.php
blob: f6d0264df9abc8d0bf0b35de53791a3d3d3efc8a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?php
	

	class Template {
		var $s;
		var $r;
		var $search;
		var $replace;
		
		private function _build_replace($r, $prefix){
	
			if(is_array($r) && count($r)) {
				foreach ($r as $k => $v ) {
					if (is_array($v))
						$this->_build_replace($v, "$prefix$k.");
					
					$this->search[] =  $prefix . $k;
					$this->replace[] = $v;
				}
			}
		} 
		
		private function _replcb_if($m){
			//echo "<pre>"; var_dump($m);
			$keys = explode(".",$m[1]);
			$val = $this->r;
			foreach($keys as $k) {
				$val = $val[$k];
			}
			
			//echo $val;
			return ($val?$m[2]:"");
		}
		
		public function replace($s, $r) {
			$this->s = $s;
			$this->r = $r;
			$this->search = array();
			$this->replace = array();
	
			$this->_build_replace($r, "");
	
			
			$s = preg_replace_callback("|{{ *if *([^ }]*) *}}([^{]*){{ *endif *}}|", array($this, "_replcb_if"), $s);
			
			return str_replace($this->search,$this->replace,$s);
		}
		
	}	
	$t = new Template;