2 define ("KEY_NOT_EXISTS", '^R_key_not_Exists^');
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();
32 private function _push_stack(){
33 $this->stack[] = array($this->r, $this->nodes);
35 private function _pop_stack(){
36 list($this->r, $this->nodes) = array_pop($this->stack);
40 private function _get_var($name, $retNoKey=false){
41 $keys = array_map('trim',explode(".",$name));
42 if ($retNoKey && !array_key_exists($keys[0], $this->r)) return KEY_NOT_EXISTS;
44 foreach($keys as $k) {
45 $val = (isset($val[$k]) ? $val[$k] : null);
53 * {{ if <$var> }}...[{{ else }} ...] {{ endif }}
54 * {{ if <$var>==<val|$var> }}...[{{ else }} ...]{{ endif }}
55 * {{ if <$var>!=<val|$var> }}...[{{ else }} ...]{{ endif }}
57 private function _replcb_if($args){
58 if (strpos($args[2],"==")>0){
59 list($a,$b) = array_map("trim",explode("==",$args[2]));
60 $a = $this->_get_var($a);
61 if ($b[0]=="$") $b = $this->_get_var($b);
63 } else if (strpos($args[2],"!=")>0){
64 list($a,$b) = explode("!=",$args[2]);
65 $a = $this->_get_var($a);
66 if ($b[0]=="$") $b = $this->_get_var($b);
69 $val = $this->_get_var($args[2]);
71 $x = preg_split("|{{ *else *}}|", $args[3]);
72 return ( $val ? $x[0] : (isset($x[1]) ? $x[1] : ""));
78 * {{ for <$var> as $name }}...{{ endfor }}
79 * {{ for <$var> as $key=>$name }}...{{ endfor }}
81 private function _replcb_for($args){
82 $m = array_map('trim', explode(" as ", $args[2]));
83 $x = explode("=>",$m[1]);
88 list($keyname, $varname) = $x;
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 === 0) ? '0' : $k);
100 $ret .= $this->replace($args[3], $r);
109 * {{ inc <templatefile> [with $var1=$var2] }}{{ endinc }}
111 private function _replcb_inc($args){
112 if (strpos($args[2],"with")) {
113 list($tplfile, $newctx) = array_map('trim', explode("with",$args[2]));
115 $tplfile = trim($args[2]);
119 if ($tplfile[0]=="$") $tplfile = $this->_get_var($tplfile);
121 $this->_push_stack();
123 if (!is_null($newctx)) {
124 list($a,$b) = array_map('trim', explode("=",$newctx));
125 $r[$a] = $this->_get_var($b);
127 $this->nodes = Array();
128 $tpl = get_markup_template($tplfile);
129 $ret = $this->replace($tpl, $r);
135 private function _replcb_node($m) {
136 $node = $this->nodes[$m[1]];
137 if (method_exists($this, "_replcb_".$node[1])){
138 $s = call_user_func(array($this, "_replcb_".$node[1]), $node);
142 $s = preg_replace_callback('/\|\|([0-9]+)\|\|/', array($this, "_replcb_node"), $s);
146 private function _replcb($m){
147 //var_dump(array_map('htmlspecialchars', $m));
149 $this->nodes[] = (array) $m;
150 return "||". (count($this->nodes)-1) ."||";
153 private function _build_nodes($s){
155 while (!$this->done){
157 $s = preg_replace_callback('|{{ *([a-z]*) *([^}]*)}}([^{]*({{ *else *}}[^{]*)?){{ *end\1 *}}|', array($this, "_replcb"), $s);
158 if ($s==Null) $this->_preg_error();
160 //({{ *else *}}[^{]*)?
161 krsort($this->nodes);
166 private function var_replace($s){
170 * (\[)? optional open square bracket
171 * ([a-zA-Z0-9-_]+\.?)+ var name, followed by optional
172 * dot, repeated at least 1 time
173 * (?(1)\]) if there was opened square bracket
174 * (subgrup 1), match close bracket
176 if (preg_match_all('/\$(\[)?([a-zA-Z0-9-_]+\.?)+(?(1)\])/', $s,$m)){
178 foreach($m[0] as $var){
179 $varn = str_replace(array("[","]"), array("",""), $var);
180 $val = $this->_get_var($varn, true);
181 if ($val!=KEY_NOT_EXISTS)
182 $s = str_replace($var, $val, $s);
189 public function replace($s, $r) {
192 $s = $this->_build_nodes($s);
194 $s = preg_replace_callback('/\|\|([0-9]+)\|\|/', array($this, "_replcb_node"), $s);
195 if ($s==Null) $this->_preg_error();
197 // remove comments block
198 $s = preg_replace('/{#[^#]*#}/', "" , $s);
200 // replace strings recursively (limit to 10 loops)
202 while($os!=$s && $count<10){
204 $s = $this->var_replace($s);
215 function template_escape($s) {
217 return str_replace(array('$','{{'),array('!_Doll^Ars1Az_!','!_DoubLe^BraceS4Rw_!'),$s);
222 function template_unescape($s) {
224 return str_replace(array('!_Doll^Ars1Az_!','!_DoubLe^BraceS4Rw_!'),array('$','{{'),$s);