15 private function _preg_error(){
16 switch(preg_last_error()){
17 case PREG_INTERNAL_ERROR: echo('PREG_INTERNAL_ERROR'); break;
18 case PREG_BACKTRACK_LIMIT_ERROR: echo('PREG_BACKTRACK_LIMIT_ERROR'); break;
19 case PREG_RECURSION_LIMIT_ERROR: echo('PREG_RECURSION_LIMIT_ERROR'); break;
20 case PREG_BAD_UTF8_ERROR: echo('PREG_BAD_UTF8_ERROR'); break;
21 case PREG_BAD_UTF8_OFFSET_ERROR: echo('PREG_BAD_UTF8_OFFSET_ERROR'); break;
23 //die("Unknown preg error.");
27 debug_print_backtrace();
31 private function _build_replace($r, $prefix){
33 if(is_array($r) && count($r)) {
34 foreach ($r as $k => $v ) {
36 $this->_build_replace($v, "$prefix$k.");
38 $this->search[] = $prefix . $k;
39 $this->replace[] = $v;
45 private function _push_stack(){
46 $this->stack[] = array($this->r, $this->search, $this->replace, $this->nodes);
48 private function _pop_stack(){
49 list($this->r, $this->search, $this->replace, $this->nodes) = array_pop($this->stack);
53 private function _get_var($name){
54 $keys = array_map('trim',explode(".",$name));
56 foreach($keys as $k) {
57 $val = (isset($val[$k]) ? $val[$k] : null);
65 * {{ if <$var> }}...[{{ else }} ...] {{ endif }}
66 * {{ if <$var>==<val|$var> }}...[{{ else }} ...]{{ endif }}
67 * {{ if <$var>!=<val|$var> }}...[{{ else }} ...]{{ endif }}
69 private function _replcb_if($args){
70 if (strpos($args[2],"==")>0){
71 list($a,$b) = array_map("trim",explode("==",$args[2]));
72 $a = $this->_get_var($a);
73 if ($b[0]=="$") $b = $this->_get_var($b);
75 } else if (strpos($args[2],"!=")>0){
76 list($a,$b) = explode("!=",$args[2]);
77 $a = $this->_get_var($a);
78 if ($b[0]=="$") $b = $this->_get_var($b);
81 $val = $this->_get_var($args[2]);
83 $x = preg_split("|{{ *else *}}|", $args[3]);
84 return ( $val ? $x[0] : (isset($x[1]) ? $x[1] : ""));
90 * {{ for <$var> as $name }}...{{ endfor }}
91 * {{ for <$var> as $key=>$name }}...{{ endfor }}
93 private function _replcb_for($args){
94 $m = array_map('trim', explode(" as ", $args[2]));
95 list($keyname, $varname) = explode("=>",$m[1]);
96 if (is_null($varname)) { $varname=$keyname; $keyname=""; }
97 if ($m[0]=="" || $varname=="" || is_null($varname)) die("template error: 'for ".$m[0]." as ".$varname."'") ;
98 //$vals = $this->r[$m[0]];
99 $vals = $this->_get_var($m[0]);
101 if (!is_array($vals)) return $ret;
102 foreach ($vals as $k=>$v){
103 $this->_push_stack();
106 if ($keyname!='') $r[$keyname] = $k;
107 $ret .= $this->replace($args[3], $r);
116 * {{ inc <templatefile> [with $var1=$var2] }}{{ endinc }}
118 private function _replcb_inc($args){
119 list($tplfile, $newctx) = array_map('trim', explode("with",$args[2]));
120 $this->_push_stack();
122 if (!is_null($newctx)) {
123 list($a,$b) = array_map('trim', explode("=",$newctx));
124 $r[$a] = $this->_get_var($b);
126 $this->nodes = Array();
127 $tpl = get_markup_template($tplfile);
128 $ret = $this->replace($tpl, $r);
134 private function _replcb_node($m) {
135 $node = $this->nodes[$m[1]];
136 if (method_exists($this, "_replcb_".$node[1])){
137 $s = call_user_func(array($this, "_replcb_".$node[1]), $node);
141 $s = preg_replace_callback('/\|\|([0-9]+)\|\|/', array($this, "_replcb_node"), $s);
145 private function _replcb($m){
146 //var_dump(array_map('htmlspecialchars', $m));
148 $this->nodes[] = (array) $m;
149 return "||". (count($this->nodes)-1) ."||";
152 private function _build_nodes($s){
154 while (!$this->done){
156 $s = preg_replace_callback('|{{ *([a-z]*) *([^}]*)}}([^{]*({{ *else *}}[^{]*)?){{ *end\1 *}}|', array($this, "_replcb"), $s);
157 if ($s==Null) $this->_preg_error();
159 //({{ *else *}}[^{]*)?
160 krsort($this->nodes);
165 private function _str_replace($str){
166 #$this->search,$this->replace,
167 $searchs = $this->search;
168 foreach($searchs as $search){
169 $search = "|".preg_quote($search)."(\|[a-zA-Z0-9_]*)*|";
171 if (preg_match_all($search, $str,$m)){
172 foreach ($m[0] as $match){
173 $toks = explode("|",$match);
174 $val = $this->_get_var($toks[0]);
175 for($k=1; $k<count($toks); $k++){
177 if (function_exists($func)) $val = $func($val);
180 $str = str_replace( $match, $val, $str);
186 return str_replace($this->search,$this->replace, $str);
190 public function replace($s, $r) {
192 $this->search = array();
193 $this->replace = array();
195 $this->_build_replace($r, "");
197 #$s = str_replace(array("\n","\r"),array("§n§","§r§"),$s);
198 $s = $this->_build_nodes($s);
200 $s = preg_replace_callback('/\|\|([0-9]+)\|\|/', array($this, "_replcb_node"), $s);
201 if ($s==Null) $this->_preg_error();
203 // remove comments block
204 $s = preg_replace('/{#[^#]*#}/', "" , $s);
205 // replace strings recursively (limit to 10 loops)
207 while($os!=$s && $count<10){
209 //$s = $this->_str_replace($s);
210 $s = str_replace($this->search, $this->replace, $s);
212 return template_unescape($s);
221 function template_escape($s) {
223 return str_replace(array('$','{{'),array('!_Doll^Ars1Az_!','!_DoubLe^BraceS4Rw_!'),$s);
228 function template_unescape($s) {
230 return str_replace(array('!_Doll^Ars1Az_!','!_DoubLe^BraceS4Rw_!'),array('$','{{'),$s);