12 private function _preg_error(){
13 switch(preg_last_error()){
14 case PREG_INTERNAL_ERROR: die('PREG_INTERNAL_ERROR'); break;
15 case PREG_BACKTRACK_LIMIT_ERROR: die('PREG_BACKTRACK_LIMIT_ERROR'); break;
16 case PREG_RECURSION_LIMIT_ERROR: die('PREG_RECURSION_LIMIT_ERROR'); break;
17 case PREG_BAD_UTF8_ERROR: die('PREG_BAD_UTF8_ERROR'); break;
18 case PREG_BAD_UTF8_OFFSET_ERROR: die('PREG_BAD_UTF8_OFFSET_ERROR'); break;
20 //die("Unknown preg error.");
25 private function _build_replace($r, $prefix){
27 if(is_array($r) && count($r)) {
28 foreach ($r as $k => $v ) {
30 $this->_build_replace($v, "$prefix$k.");
32 $this->search[] = $prefix . $k;
33 $this->replace[] = $v;
38 private function _push_stack(){
39 $this->stack[] = array($this->r, $this->search, $this->replace, $this->nodes);
41 private function _pop_stack(){
42 list($this->r, $this->search, $this->replace, $this->nodes) = array_pop($this->stack);
46 private function _get_var($name){
47 $keys = array_map('trim',explode(".",$name));
49 foreach($keys as $k) {
58 * {{ if <$var> }}...[{{ else }} ...] {{ endif }}
59 * {{ if <$var>==<val|$var> }}...[{{ else }} ...]{{ endif }}
60 * {{ if <$var>!=<val|$var> }}...[{{ else }} ...]{{ endif }}
62 private function _replcb_if($args){
63 if (strpos($args[2],"==")>0){
64 list($a,$b) = array_map("trim",explode("==",$args[2]));
65 $a = $this->_get_var($a);
66 if ($b[0]=="$") $b = $this->_get_var($b);
68 } else if (strpos($args[2],"!=")>0){
69 list($a,$b) = explode("!=",$args[2]);
70 $a = $this->_get_var($a);
71 if ($b[0]=="$") $b = $this->_get_var($b);
74 $val = $this->_get_var($args[2]);
76 list($strue, $sfalse)= preg_split("|{{ *else *}}|", $args[3]);
77 return ($val?$strue:$sfalse);
83 * {{ for <$var> as $name }}...{{ endfor }}
84 * {{ for <$var> as $key=>$name }}...{{ endfor }}
86 private function _replcb_for($args){
87 $m = array_map('trim', explode(" as ", $args[2]));
88 list($keyname, $varname) = explode("=>",$m[1]);
89 if (is_null($varname)) { $varname=$keyname; $keyname=""; }
90 if ($m[0]=="" || $varname=="" || is_null($varname)) die("template error: 'for ".$m[0]." as ".$varname."'") ;
91 //$vals = $this->r[$m[0]];
92 $vals = $this->_get_var($m[0]);
94 if (!is_array($vals)) return $ret;
95 foreach ($vals as $k=>$v){
99 if ($keyname!='') $r[$keyname] = $k;
100 $ret .= $this->replace($args[3], $r);
109 * {{ inc <templatefile> [with $var1=$var2] }}{{ endinc }}
111 private function _replcb_inc($args){
112 list($tplfile, $newctx) = array_map('trim', explode("with",$args[2]));
113 $this->_push_stack();
115 if (!is_null($newctx)) {
116 list($a,$b) = array_map('trim', explode("=",$newctx));
117 $r[$a] = $this->_get_var($b);
119 $this->nodes = Array();
120 $tpl = get_markup_template($tplfile);
121 $ret = $this->replace($tpl, $r);
127 private function _replcb_node($m) {
128 $node = $this->nodes[$m[1]];
129 if (method_exists($this, "_replcb_".$node[1])){
130 $s = call_user_func(array($this, "_replcb_".$node[1]), $node);
134 $s = preg_replace_callback('/\|\|([0-9]+)\|\|/', array($this, "_replcb_node"), $s);
138 private function _replcb($m){
139 //var_dump(array_map('htmlspecialchars', $m));
141 $this->nodes[] = (array) $m;
142 return "||". (count($this->nodes)-1) ."||";
145 private function _build_nodes($s){
147 while (!$this->done){
149 $s = preg_replace_callback('|{{ *([a-z]*) *([^}]*)}}([^{]*({{ *else *}}[^{]*)?){{ *end\1 *}}|', array($this, "_replcb"), $s);
150 if ($s==Null) $this->_preg_error();
152 //({{ *else *}}[^{]*)?
153 krsort($this->nodes);
157 public function replace($s, $r) {
159 $this->search = array();
160 $this->replace = array();
162 $this->_build_replace($r, "");
164 #$s = str_replace(array("\n","\r"),array("§n§","§r§"),$s);
165 $s = $this->_build_nodes($s);
166 $s = preg_replace_callback('/\|\|([0-9]+)\|\|/', array($this, "_replcb_node"), $s);
167 if ($s==Null) $this->_preg_error();
169 // replace strings recursively (limit to 10 loops)
171 while($os!=$s && $count<10){
173 $s = str_replace($this->search,$this->replace, $s);