15 private function _preg_error(){
16 switch(preg_last_error()){
17 case PREG_INTERNAL_ERROR: die('PREG_INTERNAL_ERROR'); break;
18 case PREG_BACKTRACK_LIMIT_ERROR: die('PREG_BACKTRACK_LIMIT_ERROR'); break;
19 case PREG_RECURSION_LIMIT_ERROR: die('PREG_RECURSION_LIMIT_ERROR'); break;
20 case PREG_BAD_UTF8_ERROR: die('PREG_BAD_UTF8_ERROR'); break;
21 case PREG_BAD_UTF8_OFFSET_ERROR: die('PREG_BAD_UTF8_OFFSET_ERROR'); break;
23 //die("Unknown preg error.");
28 private function _build_replace($r, $prefix){
30 if(is_array($r) && count($r)) {
31 foreach ($r as $k => $v ) {
33 $this->_build_replace($v, "$prefix$k.");
35 $this->_build_replace($v->getKeys(), "$prefix$k.");
37 $this->search[] = $prefix . $k;
38 $this->replace[] = $v;
43 private function _push_stack(){
44 $this->stack[] = array($this->r, $this->search, $this->replace, $this->nodes);
46 private function _pop_stack(){
47 list($this->r, $this->search, $this->replace, $this->nodes) = array_pop($this->stack);
51 private function _get_var($name){
52 $keys = array_map('trim',explode(".",$name));
54 foreach($keys as $k) {
63 * {{ if <$var> }}...[{{ else }} ...] {{ endif }}
64 * {{ if <$var>==<val|$var> }}...[{{ else }} ...]{{ endif }}
65 * {{ if <$var>!=<val|$var> }}...[{{ else }} ...]{{ endif }}
67 private function _replcb_if($args){
68 if (strpos($args[2],"==")>0){
69 list($a,$b) = array_map("trim",explode("==",$args[2]));
70 $a = $this->_get_var($a);
71 if ($b[0]=="$") $b = $this->_get_var($b);
73 } else if (strpos($args[2],"!=")>0){
74 list($a,$b) = explode("!=",$args[2]);
75 $a = $this->_get_var($a);
76 if ($b[0]=="$") $b = $this->_get_var($b);
79 $val = $this->_get_var($args[2]);
81 list($strue, $sfalse)= preg_split("|{{ *else *}}|", $args[3]);
82 return ($val?$strue:$sfalse);
88 * {{ for <$var> as $name }}...{{ endfor }}
89 * {{ for <$var> as $key=>$name }}...{{ endfor }}
91 private function _replcb_for($args){
92 $m = array_map('trim', explode(" as ", $args[2]));
93 list($keyname, $varname) = explode("=>",$m[1]);
94 if (is_null($varname)) { $varname=$keyname; $keyname=""; }
95 if ($m[0]=="" || $varname=="" || is_null($varname)) die("template error: 'for ".$m[0]." as ".$varname."'") ;
96 //$vals = $this->r[$m[0]];
97 $vals = $this->_get_var($m[0]);
99 if (!is_array($vals)) return $ret;
100 foreach ($vals as $k=>$v){
101 $this->_push_stack();
104 if ($keyname!='') $r[$keyname] = $k;
105 $ret .= $this->replace($args[3], $r);
114 * {{ inc <templatefile> [with $var1=$var2] }}{{ endinc }}
116 private function _replcb_inc($args){
117 list($tplfile, $newctx) = array_map('trim', explode("with",$args[2]));
118 $this->_push_stack();
120 if (!is_null($newctx)) {
121 list($a,$b) = array_map('trim', explode("=",$newctx));
122 $r[$a] = $this->_get_var($b);
124 $this->nodes = Array();
125 $tpl = get_markup_template($tplfile);
126 $ret = $this->replace($tpl, $r);
132 private function _replcb_node($m) {
133 $node = $this->nodes[$m[1]];
134 if (method_exists($this, "_replcb_".$node[1])){
135 $s = call_user_func(array($this, "_replcb_".$node[1]), $node);
139 $s = preg_replace_callback('/\|\|([0-9]+)\|\|/', array($this, "_replcb_node"), $s);
143 private function _replcb($m){
144 //var_dump(array_map('htmlspecialchars', $m));
146 $this->nodes[] = (array) $m;
147 return "||". (count($this->nodes)-1) ."||";
150 private function _build_nodes($s){
152 while (!$this->done){
154 $s = preg_replace_callback('|{{ *([a-z]*) *([^}]*)}}([^{]*({{ *else *}}[^{]*)?){{ *end\1 *}}|', array($this, "_replcb"), $s);
155 if ($s==Null) $this->_preg_error();
157 //({{ *else *}}[^{]*)?
158 krsort($this->nodes);
162 private function _get_lang(){
163 if ($this->lang!=null) return $this->lang;
167 if(is_array($a->strings) && count($a->strings)) {
168 foreach ($a->strings as $k=>$v){
169 $k = preg_replace("/[^a-z0-9-]/", "", str_replace(" ","-", strtolower($k)));
170 $this->lang[$k] = $v;
177 public function replace($s, $r) {
179 $r['$lang'] = &$this->_get_lang();
182 $this->search = array();
183 $this->replace = array();
184 $this->_build_replace($r, "");
185 #$s = str_replace(array("\n","\r"),array("§n§","§r§"),$s);
186 $s = $this->_build_nodes($s);
187 $s = preg_replace_callback('/\|\|([0-9]+)\|\|/', array($this, "_replcb_node"), $s);
188 if ($s==Null) $this->_preg_error();
190 // remove comments block
191 $s = preg_replace('/{#[^#]*#}/', "" , $s);
193 // replace strings recursively (limit to 10 loops)
195 while($os!=$s && $count<10){
197 $s = str_replace($this->search,$this->replace, $s);
199 return template_unescape($s);
208 function template_escape($s) {
210 return str_replace(array('$','{{'),array('!_Doll^Ars1Az_!','!_DoubLe^BraceS4Rw_!'),$s);
215 function template_unescape($s) {
217 return str_replace(array('!_Doll^Ars1Az_!','!_DoubLe^BraceS4Rw_!'),array('$','{{'),$s);