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;
44 private function _push_stack(){
45 $this->stack[] = array($this->r, $this->search, $this->replace, $this->nodes);
47 private function _pop_stack(){
48 list($this->r, $this->search, $this->replace, $this->nodes) = array_pop($this->stack);
52 private function _get_var($name){
53 $keys = array_map('trim',explode(".",$name));
55 foreach($keys as $k) {
64 * {{ if <$var> }}...[{{ else }} ...] {{ endif }}
65 * {{ if <$var>==<val|$var> }}...[{{ else }} ...]{{ endif }}
66 * {{ if <$var>!=<val|$var> }}...[{{ else }} ...]{{ endif }}
68 private function _replcb_if($args){
69 if (strpos($args[2],"==")>0){
70 list($a,$b) = array_map("trim",explode("==",$args[2]));
71 $a = $this->_get_var($a);
72 if ($b[0]=="$") $b = $this->_get_var($b);
74 } else if (strpos($args[2],"!=")>0){
75 list($a,$b) = explode("!=",$args[2]);
76 $a = $this->_get_var($a);
77 if ($b[0]=="$") $b = $this->_get_var($b);
80 $val = $this->_get_var($args[2]);
82 list($strue, $sfalse)= preg_split("|{{ *else *}}|", $args[3]);
83 return ($val?$strue:$sfalse);
89 * {{ for <$var> as $name }}...{{ endfor }}
90 * {{ for <$var> as $key=>$name }}...{{ endfor }}
92 private function _replcb_for($args){
93 $m = array_map('trim', explode(" as ", $args[2]));
94 list($keyname, $varname) = explode("=>",$m[1]);
95 if (is_null($varname)) { $varname=$keyname; $keyname=""; }
96 if ($m[0]=="" || $varname=="" || is_null($varname)) die("template error: 'for ".$m[0]." as ".$varname."'") ;
97 //$vals = $this->r[$m[0]];
98 $vals = $this->_get_var($m[0]);
100 if (!is_array($vals)) return $ret;
101 foreach ($vals as $k=>$v){
102 $this->_push_stack();
105 if ($keyname!='') $r[$keyname] = $k;
106 $ret .= $this->replace($args[3], $r);
115 * {{ inc <templatefile> [with $var1=$var2] }}{{ endinc }}
117 private function _replcb_inc($args){
118 list($tplfile, $newctx) = array_map('trim', explode("with",$args[2]));
119 $this->_push_stack();
121 if (!is_null($newctx)) {
122 list($a,$b) = array_map('trim', explode("=",$newctx));
123 $r[$a] = $this->_get_var($b);
125 $this->nodes = Array();
126 $tpl = get_markup_template($tplfile);
127 $ret = $this->replace($tpl, $r);
133 private function _replcb_node($m) {
134 $node = $this->nodes[$m[1]];
135 if (method_exists($this, "_replcb_".$node[1])){
136 $s = call_user_func(array($this, "_replcb_".$node[1]), $node);
140 $s = preg_replace_callback('/\|\|([0-9]+)\|\|/', array($this, "_replcb_node"), $s);
144 private function _replcb($m){
145 //var_dump(array_map('htmlspecialchars', $m));
147 $this->nodes[] = (array) $m;
148 return "||". (count($this->nodes)-1) ."||";
151 private function _build_nodes($s){
153 while (!$this->done){
155 $s = preg_replace_callback('|{{ *([a-z]*) *([^}]*)}}([^{]*({{ *else *}}[^{]*)?){{ *end\1 *}}|', array($this, "_replcb"), $s);
156 if ($s==Null) $this->_preg_error();
158 //({{ *else *}}[^{]*)?
159 krsort($this->nodes);
164 public function replace($s, $r) {
166 $this->search = array();
167 $this->replace = array();
169 $this->_build_replace($r, "");
171 #$s = str_replace(array("\n","\r"),array("§n§","§r§"),$s);
172 $s = $this->_build_nodes($s);
174 $s = preg_replace_callback('/\|\|([0-9]+)\|\|/', array($this, "_replcb_node"), $s);
175 if ($s==Null) $this->_preg_error();
177 // remove comments block
178 $s = preg_replace('/{#[^#]*#}/', "" , $s);
179 // replace strings recursively (limit to 10 loops)
181 while($os!=$s && $count<10){
183 $s = str_replace($this->search,$this->replace, $s);
185 return template_unescape($s);
194 function template_escape($s) {
196 return str_replace(array('$','{{'),array('!_Doll^Ars1Az_!','!_DoubLe^BraceS4Rw_!'),$s);
201 function template_unescape($s) {
203 return str_replace(array('!_Doll^Ars1Az_!','!_DoubLe^BraceS4Rw_!'),array('$','{{'),$s);