"; var $block_start_word="BEGIN:"; var $block_end_word="END:"; var $NULL_STRING=array(""=>""); var $NULL_BLOCK=array(""=>""); var $mainblock=""; var $ERROR=""; var $AUTORESET=1; function XTemplate ($file,$mainblock="main") { $this->mainblock=$mainblock; $this->filecontents=$this->r_getfile($file); $this->blocks=$this->maketree($this->filecontents,$mainblock); $this->scan_globals(); } function assign ($name,$val="") { if (gettype($name)=="array") while (list($k,$v)=each($name)) $this->VARS[$k]=$v; else $this->VARS[$name]=$val; } function parse ($bname) { $copy=$this->blocks[$bname]; if (!isset($this->blocks[$bname])) $this->set_error ("parse: blockname [$bname] does not exist"); preg_match_all("/\{([A-Za-z0-9\._]+?)}/",$this->blocks[$bname],$var_array); $var_array=$var_array[1]; while (list($k,$v)=each($var_array)) { $sub=explode(".",$v); if ($sub[0]=="_BLOCK_") { unset($sub[0]); $bname2=implode(".",$sub); $var=$this->parsed_blocks[$bname2]; $nul=(!isset($this->NULL_BLOCK[$bname2])) ? $this->NULL_BLOCK[""] : $this->NULL_BLOCK[$bname2]; $var=(empty($var))?$nul:trim($var); $copy=preg_replace("/\{".$v."\}/","$var",$copy); } else { $var=$this->VARS; while(list($k1,$v1)=each($sub)) $var=$var[$v1]; $nul=(!isset($this->NULL_STRING[$v])) ? ($this->NULL_STRING[""]) : ($this->NULL_STRING[$v]); $var=(!isset($var))?$nul:$var; $copy=preg_replace("/\{$v\}/","$var",$copy); } } $this->parsed_blocks[$bname].=$copy; if ($this->AUTORESET && (!empty($this->sub_blocks[$bname]))) { reset($this->sub_blocks[$bname]); while (list($k,$v)=each($this->sub_blocks[$bname])) $this->reset($v); } } function rparse($bname) { if (!empty($this->sub_blocks[$bname])) { reset($this->sub_blocks[$bname]); while (list($k,$v)=each($this->sub_blocks[$bname])) if (!empty($v)) $this->rparse($v,$indent."\t"); } $this->parse($bname); } function insert_loop($bname,$var,$value="") { $this->assign($var,$value); $this->parse($bname); } function text($bname) { return $this->parsed_blocks[isset($bname) ? $bname :$this->mainblock]; } function out ($bname) { echo $this->text($bname); } function reset ($bname) { $this->parsed_blocks[$bname]=""; } function parsed ($bname) { return (!empty($this->parsed_blocks[$bname])); } function SetNullString($str,$varname="") { $this->NULL_STRING[$varname]=$str; } function SetNullBlock($str,$bname="") { $this->NULL_BLOCK[$bname]=$str; } function set_autoreset() { $this->AUTORESET=1; } function clear_autoreset() { $this->AUTORESET=0; } function scan_globals() { reset($GLOBALS); while (list($k,$v)=each($GLOBALS)) $GLOB[$k]=$v; $this->assign("PHP",$GLOB); } function maketree($con,$block) { $con2=explode($this->block_start_delim,$con); $level=0; $block_names=array(); $blocks=array(); reset($con2); while(list($k,$v)=each($con2)) { $patt="($this->block_start_word|$this->block_end_word)\s*(\w+)\s*$this->block_end_delim(.*)"; if (preg_match_all("/$patt/ims",$v,$res, PREG_SET_ORDER)) { // $res[0][1] = BEGIN or END // $res[0][2] = block name // $res[0][3] = kinda content if ($res[0][1]==$this->block_start_word) { $parent_name=implode(".",$block_names); $block_names[++$level]=$res[0][2]; $cur_block_name=implode(".",$block_names); $this->block_parse_order[]=$cur_block_name; $blocks[$cur_block_name].=$res[0][3]; $blocks[$parent_name].="{_BLOCK_.$cur_block_name}"; $this->sub_blocks[$parent_name][]=$cur_block_name; $this->sub_blocks[$cur_block_name][]=""; } else if ($res[0][1]==$this->block_end_word) { unset($block_names[$level--]); $parent_name=implode(".",$block_names); $blocks[$parent_name].=$res[0][3]; } } else { $blocks[implode(".",$block_names)].=$this->block_start_delim.$v; } } return $blocks; } function get_error() { return ($this->ERROR=="")?0:$this->ERROR; } function set_error($str) { $this->ERROR=$str; } function getfile($file) { if (!isset($file)) { $this->set_error("!isset file name!"); return ""; } if (is_file($file)) { if (!($fh=fopen($file,"r"))) { $this->set_error("Cannot open file: $file"); return ""; } $file_text=fread($fh,filesize($file)); fclose($fh); } else { $this->set_error("[$file] does not exist"); $file_text="__XTemplate fatal error: file [$file] does not exist__"; } return $file_text; } function r_getfile($file) { $text=$this->getfile($file); while (preg_match($this->file_delim,$text,$res)) { $text2=$this->getfile($res[1]); $text=preg_replace("'".preg_quote($res[0])."'",$text2,$text); } return $text; } } ?>