2 define ("KEY_NOT_EXISTS", '^R_key_not_Exists^');
15 private function _preg_error(){
17 switch(preg_last_error()){
18 case PREG_INTERNAL_ERROR: echo('PREG_INTERNAL_ERROR'); break;
19 case PREG_BACKTRACK_LIMIT_ERROR: echo('PREG_BACKTRACK_LIMIT_ERROR'); break;
20 case PREG_RECURSION_LIMIT_ERROR: echo('PREG_RECURSION_LIMIT_ERROR'); break;
21 case PREG_BAD_UTF8_ERROR: echo('PREG_BAD_UTF8_ERROR'); break;
22 // This is only valid for php > 5.3, not certain how to code around it for unit tests
23 // case PREG_BAD_UTF8_OFFSET_ERROR: echo('PREG_BAD_UTF8_OFFSET_ERROR'); break;
25 //die("Unknown preg error.");
29 debug_print_backtrace();
34 private function _push_stack(){
35 $this->stack[] = array($this->r, $this->nodes);
37 private function _pop_stack(){
38 list($this->r, $this->nodes) = array_pop($this->stack);
42 private function _get_var($name, $retNoKey=false){
43 $keys = array_map('trim',explode(".",$name));
44 if ($retNoKey && !array_key_exists($keys[0], $this->r)) return KEY_NOT_EXISTS;
46 foreach($keys as $k) {
47 $val = (isset($val[$k]) ? $val[$k] : null);
55 * {{ if <$var> }}...[{{ else }} ...] {{ endif }}
56 * {{ if <$var>==<val|$var> }}...[{{ else }} ...]{{ endif }}
57 * {{ if <$var>!=<val|$var> }}...[{{ else }} ...]{{ endif }}
59 private function _replcb_if($args){
60 if (strpos($args[2],"==")>0){
61 list($a,$b) = array_map("trim",explode("==",$args[2]));
62 $a = $this->_get_var($a);
63 if ($b[0]=="$") $b = $this->_get_var($b);
65 } else if (strpos($args[2],"!=")>0){
66 list($a,$b) = explode("!=",$args[2]);
67 $a = $this->_get_var($a);
68 if ($b[0]=="$") $b = $this->_get_var($b);
71 $val = $this->_get_var($args[2]);
73 $x = preg_split("|{{ *else *}}|", $args[3]);
74 return ( $val ? $x[0] : (isset($x[1]) ? $x[1] : ""));
80 * {{ for <$var> as $name }}...{{ endfor }}
81 * {{ for <$var> as $key=>$name }}...{{ endfor }}
83 private function _replcb_for($args){
84 $m = array_map('trim', explode(" as ", $args[2]));
85 $x = explode("=>",$m[1]);
90 list($keyname, $varname) = $x;
92 if ($m[0]=="" || $varname=="" || is_null($varname)) die("template error: 'for ".$m[0]." as ".$varname."'") ;
93 //$vals = $this->r[$m[0]];
94 $vals = $this->_get_var($m[0]);
96 if (!is_array($vals)) return $ret;
97 foreach ($vals as $k=>$v){
101 if ($keyname!='') $r[$keyname] = (($k === 0) ? '0' : $k);
102 $ret .= $this->replace($args[3], $r);
111 * {{ inc <templatefile> [with $var1=$var2] }}{{ endinc }}
113 private function _replcb_inc($args){
114 if (strpos($args[2],"with")) {
115 list($tplfile, $newctx) = array_map('trim', explode("with",$args[2]));
117 $tplfile = trim($args[2]);
121 if ($tplfile[0]=="$") $tplfile = $this->_get_var($tplfile);
123 $this->_push_stack();
125 if (!is_null($newctx)) {
126 list($a,$b) = array_map('trim', explode("=",$newctx));
127 $r[$a] = $this->_get_var($b);
129 $this->nodes = Array();
130 $tpl = get_markup_template($tplfile);
131 $ret = $this->replace($tpl, $r);
137 private function _replcb_node($m) {
138 $node = $this->nodes[$m[1]];
139 if (method_exists($this, "_replcb_".$node[1])){
140 $s = call_user_func(array($this, "_replcb_".$node[1]), $node);
144 $s = preg_replace_callback('/\|\|([0-9]+)\|\|/', array($this, "_replcb_node"), $s);
148 private function _replcb($m){
149 //var_dump(array_map('htmlspecialchars', $m));
151 $this->nodes[] = (array) $m;
152 return "||". (count($this->nodes)-1) ."||";
155 private function _build_nodes($s){
157 while (!$this->done){
159 $s = preg_replace_callback('|{{ *([a-z]*) *([^}]*)}}([^{]*({{ *else *}}[^{]*)?){{ *end\1 *}}|', array($this, "_replcb"), $s);
160 if ($s==Null) $this->_preg_error();
162 //({{ *else *}}[^{]*)?
163 krsort($this->nodes);
168 private function var_replace($s){
172 * (\[)? optional open square bracket
173 * ([a-zA-Z0-9-_]+\.?)+ var name, followed by optional
174 * dot, repeated at least 1 time
175 * (?(1)\]) if there was opened square bracket
176 * (subgrup 1), match close bracket
178 if (preg_match_all('/\$(\[)?([a-zA-Z0-9-_]+\.?)+(?(1)\])/', $s,$m)){
180 foreach($m[0] as $var){
181 $varn = str_replace(array("[","]"), array("",""), $var);
182 $val = $this->_get_var($varn, true);
183 if ($val!=KEY_NOT_EXISTS)
184 $s = str_replace($var, $val, $s);
191 public function replace($s, $r) {
194 $s = $this->_build_nodes($s);
196 $s = preg_replace_callback('/\|\|([0-9]+)\|\|/', array($this, "_replcb_node"), $s);
197 if ($s==Null) $this->_preg_error();
199 // remove comments block
200 $s = preg_replace('/{#[^#]*#}/', "" , $s);
202 // replace strings recursively (limit to 10 loops)
204 while($os!=$s && $count<10){
206 $s = $this->var_replace($s);
217 function template_escape($s) {
219 return str_replace(array('$','{{'),array('!_Doll^Ars1Az_!','!_DoubLe^BraceS4Rw_!'),$s);
224 function template_unescape($s) {
226 return str_replace(array('!_Doll^Ars1Az_!','!_DoubLe^BraceS4Rw_!'),array('$','{{'),$s);