12 private function _preg_error(){
13 switch(preg_last_error()){
14 case PREG_INTERNAL_ERROR: echo('PREG_INTERNAL_ERROR'); break;
15 case PREG_BACKTRACK_LIMIT_ERROR: echo('PREG_BACKTRACK_LIMIT_ERROR'); break;
16 case PREG_RECURSION_LIMIT_ERROR: echo('PREG_RECURSION_LIMIT_ERROR'); break;
17 case PREG_BAD_UTF8_ERROR: echo('PREG_BAD_UTF8_ERROR'); break;
18 case PREG_BAD_UTF8_OFFSET_ERROR: echo('PREG_BAD_UTF8_OFFSET_ERROR'); break;
20 //die("Unknown preg error.");
24 debug_print_backtrace();
28 private function _build_replace($r, $prefix){
30 if(is_array($r) && count($r)) {
31 foreach ($r as $k => $v ) {
33 $this->_build_replace($v, "$prefix$k.");
35 $this->search[] = $prefix . $k;
36 $this->replace[] = $v;
41 private function _push_stack(){
42 $this->stack[] = array($this->r, $this->search, $this->replace, $this->nodes);
44 private function _pop_stack(){
45 list($this->r, $this->search, $this->replace, $this->nodes) = array_pop($this->stack);
49 private function _get_var($name){
50 $keys = array_map('trim',explode(".",$name));
52 foreach($keys as $k) {
61 * {{ if <$var> }}...[{{ else }} ...] {{ endif }}
62 * {{ if <$var>==<val|$var> }}...[{{ else }} ...]{{ endif }}
63 * {{ if <$var>!=<val|$var> }}...[{{ else }} ...]{{ endif }}
65 private function _replcb_if($args){
66 if (strpos($args[2],"==")>0){
67 list($a,$b) = array_map("trim",explode("==",$args[2]));
68 $a = $this->_get_var($a);
69 if ($b[0]=="$") $b = $this->_get_var($b);
71 } else if (strpos($args[2],"!=")>0){
72 list($a,$b) = explode("!=",$args[2]);
73 $a = $this->_get_var($a);
74 if ($b[0]=="$") $b = $this->_get_var($b);
77 $val = $this->_get_var($args[2]);
79 list($strue, $sfalse)= preg_split("|{{ *else *}}|", $args[3]);
80 return ($val?$strue:$sfalse);
86 * {{ for <$var> as $name }}...{{ endfor }}
87 * {{ for <$var> as $key=>$name }}...{{ endfor }}
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]);
97 if (!is_array($vals)) return $ret;
98 foreach ($vals as $k=>$v){
102 if ($keyname!='') $r[$keyname] = $k;
103 $ret .= $this->replace($args[3], $r);
112 * {{ inc <templatefile> [with $var1=$var2] }}{{ endinc }}
114 private function _replcb_inc($args){
115 list($tplfile, $newctx) = array_map('trim', explode("with",$args[2]));
116 $this->_push_stack();
118 if (!is_null($newctx)) {
119 list($a,$b) = array_map('trim', explode("=",$newctx));
120 $r[$a] = $this->_get_var($b);
122 $this->nodes = Array();
123 $tpl = get_markup_template($tplfile);
124 $ret = $this->replace($tpl, $r);
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);
137 $s = preg_replace_callback('/\|\|([0-9]+)\|\|/', array($this, "_replcb_node"), $s);
141 private function _replcb($m){
142 //var_dump(array_map('htmlspecialchars', $m));
144 $this->nodes[] = (array) $m;
145 return "||". (count($this->nodes)-1) ."||";
148 private function _build_nodes($s){
150 while (!$this->done){
152 $s = preg_replace_callback('|{{ *([a-z]*) *([^}]*)}}([^{]*({{ *else *}}[^{]*)?){{ *end\1 *}}|', array($this, "_replcb"), $s);
153 if ($s==Null) $this->_preg_error();
155 //({{ *else *}}[^{]*)?
156 krsort($this->nodes);
160 public function replace($s, $r) {
162 $this->search = array();
163 $this->replace = array();
165 $this->_build_replace($r, "");
167 #$s = str_replace(array("\n","\r"),array("§n§","§r§"),$s);
168 $s = $this->_build_nodes($s);
169 $s = preg_replace_callback('/\|\|([0-9]+)\|\|/', array($this, "_replcb_node"), $s);
170 if ($s==Null) $this->_preg_error();
172 // remove comments block
173 $s = preg_replace('/{#[^#]*#}/', "" , $s);
175 // replace strings recursively (limit to 10 loops)
177 while($os!=$s && $count<10){
179 $s = str_replace($this->search,$this->replace, $s);