11 private function _preg_error(){
12 switch(preg_last_error()){
13 case PREG_INTERNAL_ERROR: die('PREG_INTERNAL_ERROR'); break;
14 case PREG_BACKTRACK_LIMIT_ERROR: die('PREG_BACKTRACK_LIMIT_ERROR'); break;
15 case PREG_RECURSION_LIMIT_ERROR: die('PREG_RECURSION_LIMIT_ERROR'); break;
16 case PREG_BAD_UTF8_ERROR: die('PREG_BAD_UTF8_ERROR'); break;
17 case PREG_BAD_UTF8_OFFSET_ERROR: die('PREG_BAD_UTF8_OFFSET_ERROR'); break;
19 die("Unknown preg error.");
23 private function _build_replace($r, $prefix){
25 if(is_array($r) && count($r)) {
26 foreach ($r as $k => $v ) {
28 $this->_build_replace($v, "$prefix$k.");
30 $this->search[] = $prefix . $k;
31 $this->replace[] = $v;
36 private function _push_stack(){
37 $this->stack[] = array($this->r, $this->search, $this->replace, $this->nodes);
39 private function _pop_stack(){
40 list($this->r, $this->search, $this->replace, $this->nodes) = array_pop($this->stack);
43 private function _get_var($name){
44 $keys = array_map('trim',explode(".",$name));
46 foreach($keys as $k) {
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){
61 if (strpos($args[2],"==")>0){
62 list($a,$b) = array_map("trim",explode("==",$args[2]));
63 $a = $this->_get_var($a);
64 if ($b[0]=="$") $b = $this->_get_var($b);
66 } else if (strpos($args[2],"!=")>0){
67 list($a,$b) = explode("!=",$args[2]);
68 $a = $this->_get_var($a);
69 if ($b[0]=="$") $b = $this->_get_var($b);
72 $val = $this->_get_var($args[2]);
74 list($strue, $sfalse)= preg_split("|{{ *else *}}|", $args[3]);
76 return ($val?$strue:$sfalse);
82 * {{ for <$var> as $name }}...{{ endfor }}
83 * {{ for <$var> as $key=>$name }}...{{ endfor }}
85 private function _replcb_for($args){
86 $m = array_map('trim', explode(" as ", $args[2]));
87 list($keyname, $varname) = explode("=>",$m[1]);
88 if (is_null($varname)) { $varname=$keyname; $keyname=""; }
89 if ($m[0]=="" || $varname=="" || is_null($varname)) die("template error: 'for ".$m[0]." as ".$varname."'") ;
90 //$vals = $this->r[$m[0]];
91 $vals = $this->_get_var($m[0]);
93 if (!is_array($vals)) return $ret;
94 foreach ($vals as $k=>$v){
98 if ($keyname!='') $r[$keyname] = $k;
99 $ret .= $this->replace($args[3], $r);
108 * {{ inc <templatefile> [with $var1=$var2] }}{{ endinc }}
110 private function _replcb_inc($args){
111 list($tplfile, $newctx) = array_map('trim', explode("with",$args[2]));
112 $this->_push_stack();
114 if (!is_null($newctx)) {
115 list($a,$b) = array_map('trim', explode("=",$newctx));
116 $r[$a] = $this->_get_var($b);
118 $this->nodes = Array();
119 $tpl = get_markup_template($tplfile);
120 $ret = $this->replace($tpl, $r);
126 private function _replcb_node($m) {
127 $node = $this->nodes[$m[1]];
128 if (method_exists($this, "_replcb_".$node[1])){
129 $s = call_user_func(array($this, "_replcb_".$node[1]), $node);
133 $s = preg_replace_callback('/\|\|([0-9]+)\|\|/', array($this, "_replcb_node"), $s);
137 private function _replcb($m){
138 //var_dump(array_map('htmlspecialchars', $m));
140 $this->nodes[] = (array) $m;
141 return "||". (count($this->nodes)-1) ."||";
144 private function _build_nodes($s){
146 while (!$this->done){
148 $s = preg_replace_callback('|{{ *([a-z]*) *([^}]*)}}([^{]*({{ *else *}}[^{]*)?){{ *end\1 *}}|', array($this, "_replcb"), $s);
149 if ($s==Null) $this->_preg_error();
151 //({{ *else *}}[^{]*)?
152 krsort($this->nodes);
156 public function replace($s, $r) {
158 $this->search = array();
159 $this->replace = array();
161 $this->_build_replace($r, "");
163 #$s = str_replace(array("\n","\r"),array("§n§","§r§"),$s);
164 $s = $this->_build_nodes($s);
165 $s = preg_replace_callback('/\|\|([0-9]+)\|\|/', array($this, "_replcb_node"), $s);
166 if ($s==Null) $this->_preg_error();
167 $s = str_replace($this->search,$this->replace, $s);