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 list($keyname, $varname) = explode("=>",$m[1]);
84 if (is_null($varname)) { $varname=$keyname; $keyname=""; }
85 if ($m[0]=="" || $varname=="" || is_null($varname)) die("template error: 'for ".$m[0]." as ".$varname."'") ;
86 //$vals = $this->r[$m[0]];
87 $vals = $this->_get_var($m[0]);
89 if (!is_array($vals)) return $ret;
90 foreach ($vals as $k=>$v){
94 if ($keyname!='') $r[$keyname] = $k;
95 $ret .= $this->replace($args[3], $r);
104 * {{ inc <templatefile> [with $var1=$var2] }}{{ endinc }}
106 private function _replcb_inc($args){
107 if (strpos($args[2],"with")) {
108 list($tplfile, $newctx) = array_map('trim', explode("with",$args[2]));
110 $tplfile = trim($args[2]);
114 if ($tplfile[0]=="$") $tplfile = $this->_get_var($tplfile);
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);
161 private function var_replace($s){
165 * (\[)? optional open square bracket
166 * ([a-zA-Z0-9-_]+\.?)+ var name, followed by optional
167 * dot, repeated at least 1 time
168 * (?(1)\]) if there was opened square bracket
169 * (subgrup 1), match close bracket
171 if (preg_match_all('/\$(\[)?([a-zA-Z0-9-_]+\.?)+(?(1)\])/', $s,$m)){
173 foreach($m[0] as $var){
174 $varn = str_replace(array("[","]"), array("",""), $var);
175 $val = $this->_get_var($varn, true);
176 if ($val!=KEY_NOT_EXISTS)
177 $s = str_replace($var, $val, $s);
184 public function replace($s, $r) {
187 $s = $this->_build_nodes($s);
189 $s = preg_replace_callback('/\|\|([0-9]+)\|\|/', array($this, "_replcb_node"), $s);
190 if ($s==Null) $this->_preg_error();
192 // remove comments block
193 $s = preg_replace('/{#[^#]*#}/', "" , $s);
195 // replace strings recursively (limit to 10 loops)
197 while($os!=$s && $count<10){
199 $s = $this->var_replace($s);
201 return template_unescape($s);
210 function template_escape($s) {
212 return str_replace(array('$','{{'),array('!_Doll^Ars1Az_!','!_DoubLe^BraceS4Rw_!'),$s);
217 function template_unescape($s) {
219 return str_replace(array('!_Doll^Ars1Az_!','!_DoubLe^BraceS4Rw_!'),array('$','{{'),$s);