|
<?PHP
//Title: //Author:Tiber //E-mail:xuzysnake@sina.com //Date:2006-12-05 //该类用来生成静态页面和HTML邮件,目前可以支持循环的数据 //该类仅支持一层的IF,IF语句要写条件快,IF判断在模板外面完成. //该类不支持嵌套的循环,一次调用只能支持一个IF和循环. /**************************************************** 调用方法: 具体的调用方法,注意里面有循环和单重的条件判断. $productList=$oColorProduct ->getProductList("cProductNumber,cProductName,cProductStyle,cProductPic1,cProductText,dCreatDate",0, 1,"cProductId='".$cProductId."'"); $staticPage = new PageStatic('detail/detail.htm');//提取模板页面 $staticPage->assign(array( 'cProductNumber'=>$productList[0]['cProductNumber'], 'cProductName'=>$productList[0]['cProductName'], 'cProductStyle'=>$productList[0]['cProductStyle'], 'dCreatDate'=>$productList[0]['dCreatDate'], 'cProductText'=>$productList[0]['cProductText'], )); $staticPage->parse_internalIf('ifimg');//分析条件判断的部分 $staticPage->assignIf ('cProductPic1', $productList[0]['cProductPic1']) ; $staticPage->setIf();//加入条件的内容 $staticPage->complier_internalIf('ifimg',(trim($productList[0]['cProductPic1'])!=''));//进行整页替换if快 $parentCategoryList=$oColorCategory->getParentCategory(); foreach($parentCategoryList as $k => $sub){ $staticPage->parse_internalFor('categoryBlock');//必须写在循环中,分析循环的部分 $staticPage->assignFor(array( 'cProductCategoryName'=>trim($sub['cProductCategoryName']), 'cProductCategoryID'=>trim($sub['cProductCategoryID']), )); $staticPage->setFor();//必须写在循环中,加入循环的部分 } $staticPage->complier_internalFor('categoryBlock');//必须写在循环外,进行整页替换循环快 $staticPage->load(1,'detail/'.$cProductId.'.htm');//最后加载一个新生成的静态页面,如果参数为空 //$mailHTML=$staticPage->load();//返处理后的HTML代码,可以用于发送HTML邮件. //$staticPage->xprint();//可以代替模板使用 ****************************************/ class PageStatic { //---设置全局变量 // Holds all assigned variable names and values. //var $VAR = array(); // 读取的模板HTMl var $HTML =''; // 读取的for循环HTML var $forHTML =''; //整合的结果for循环HTML var $result_forHtml =''; // 读取的if条件HTML var $ifHTML =''; //整合的结果if条件HTML var $result_ifHtml =''; //构造函数 function PageStatic($pathToTemplates=''){
if (!empty($pathToTemplates)) { $this->readTemplate ($pathToTemplates); } return $this;
}
/**************************************************** 函数 parse_internalIf ($forchar) $forchar--if条件的在模板中的名称 提取这部分html code such as {ifwarnBlock} ...... {/ifwarnBlock} ****************************************/ function parse_internalIf($forchar){ $s_pos =0; while(true) { $s_pos = strpos($this->HTML,'{'.$forchar.'}',$s_pos); if ($s_pos === false) break; $e_pos = strpos($this->HTML,'{/'.$forchar.'}',$s_pos); if ($e_pos === false) break; $Service_str = substr($this->HTML,$s_pos,$e_pos-$s_pos); $s_pos = $e_pos; } $this->ifHTML=str_replace('{'.$forchar.'}','',$Service_str); } /**************************************************** 函数 setIf() 加给$result_ifHtml ****************************************/ function setIf(){ $this->result_ifHtml .=$this->ifHTML; } /**************************************************** 函数 clearIf() 清空$this->ifHTML ****************************************/ function clearIf(){ $this->ifHTML =''; } /**************************************************** 函数 clearResultIf() 清空$result_ifHtml ****************************************/ function clearResultIf(){ $this->result_ifHtml =''; } /**************************************************** 函数 complier_internalIf($forchar,$contidion=true) $forchar--if条件的在模板中的名称 替换这部分html code,用在if结束后 ****************************************/ function complier_internalIf($forchar,$contidion=true){ if($contidion){ $this->HTML=ereg_replace ('{'.$forchar.'}'.'(.*)'.'{/'.$forchar.'}',$this->result_ifHtml,$this->HTML); }else{ $this->HTML=ereg_replace ('{'.$forchar.'}'.'(.*)'.'{/'.$forchar.'}','',$this->HTML); } $this->clearResultIf(); $this->clearIf(); }
/**************************************************** 函数 parse_internalFor ($forchar) $forchar--for循环的在模板中的名称 提取这部分html code such as {warnBlock} ...... {/warnBlock} ****************************************/ function parse_internalFor($forchar){ $s_pos =0; while(true) { $s_pos = strpos($this->HTML,'{'.$forchar.'}',$s_pos); if ($s_pos === false) break; $e_pos = strpos($this->HTML,'{/'.$forchar.'}',$s_pos); if ($e_pos === false) break; $Service_str = substr($this->HTML,$s_pos,$e_pos-$s_pos); $s_pos = $e_pos; } $this->forHTML=str_replace('{'.$forchar.'}','',$Service_str); } /**************************************************** 函数 setFor() 加给this->result_forHtml ****************************************/ function setFor(){ $this->result_forHtml .=$this->forHTML; } /**************************************************** 函数 clearFor() 清空$this->forHTML ****************************************/ function clearFor(){ $this->forHTML =''; } /**************************************************** 函数 clearResultFor() 清空$this->result_forHtml ****************************************/ function clearResultFor(){ $this->result_forHtml =''; } /**************************************************** 函数 complier_internalFor($forchar) $forchar--for循环的在模板中的名称 替换这部分html code,用在循环结束后 ****************************************/ function complier_internalFor($forchar){ $this->HTML=ereg_replace ('{'.$forchar.'}'.'(.*)'.'{/'.$forchar.'}',$this->result_forHtml,$this->HTML); $this->clearResultFor(); $this->clearFor(); } /**************************************************** 函数 assign ($tplkey, $style=0,$rest='') $tplkey赋入替换变量数组,$style替换的类型,0--模板HTMl, 1--读取的for循环HTML,2--读取的if循环HTML $rest--当$tplkey是变量时候,$rest是替换的值 ****************************************/ function assign ($tplkey, $style = 0,$rest='') { // print_r($tplkey); if (gettype ($tplkey) == 'array') { reset ($tplkey); while (list($key,$val) = each ($tplkey)) { if (!empty($key)) { $key = '{'.$key.'}'; if($style == 1){ $this->forHTML=str_replace($key,$val,$this->forHTML); }elseif($style == 2){ $this->ifHTML=str_replace($key,$val,$this->ifHTML); }elseif($style == 0){ $this->HTML=str_replace($key,$val,$this->HTML); }else{ return false; } } } }else { if (!empty($tplkey)) { $tplkey = '{'.$tplkey.'}'; if($style == 1){ $this->forHTML=str_replace($tplkey,$rest,$this->forHTML); }elseif($style == 2){ $this->ifHTML=str_replace($tplkey,$rest,$this->ifHTML); }elseif($style == 0){ $this->HTML=str_replace($tplkey,$rest,$this->HTML); }else{ return false; } } } }
/**************************************************** 函数 assignFor ($tplkey,$rest='') $tplkey赋入替换变量数组,$rest是替换的值 ****************************************/ function assignFor ($tplkey,$rest='') { if (gettype ($tplkey) == 'array') { $this->assign($tplkey,1); }else { if(!empty($tplkey)&&!empty($rest)){ $this->assign($tplkey,1,$rest); }
} }
/**************************************************** 函数 assignFor ($tplkey,$rest='') $tplkey赋入替换变量数组,$rest是替换的值 ****************************************/ function assignIf ($tplkey,$rest='') { if (gettype ($tplkey) == 'array') { $this->assign($tplkey,2); }else { if(!empty($tplkey)&&!empty($rest)){ $this->assign($tplkey,2,$rest); }
} } /**************************************************** 函数readTemplate($filename='') 读取模板文件,保存到html代码中 ****************************************/
function readTemplate($filename=''){ $htmlContent=''; @$handle1 = fopen($filename,"r"); do { @$data1 = fread($handle1, 9216); if (strlen($data1) == 0) { break; } $htmlContent .= $data1;
} while(true); $this->HTML=$htmlContent; } /**************************************************** 函数load($style=0,$filename='') 输出结果HTML,$style=0是输出HTML,用来发送HTML邮件, $style=1是输出HTML写入指定文件,创建html页面 ****************************************/ function load($style=0,$filename=''){ if($style==0){ return $this->HTML; }else{ $this->writeover($filename); return true; } } /**************************************************** 函数xprint() 输出结果输出模板页面,相当于FastTemple的作用. ****************************************/ function xprint(){ if(!empty($this->load())){ print $this->load(); }else{ print 'Page Error!'; } } /**************************************************** 函数writeover($filename,$data,$method="rb+",$iflock=1) 在制定目录编写静态文件 ****************************************/
function writeover($filename,$method="rb+",$iflock=1) { @touch($filename); $handle=@fopen($filename,$method); if($iflock){ flock($handle,LOCK_EX); } fputs($handle,$this->HTML); if($method=="rb+") ftruncate($handle,strlen($this->HTML)); fclose($handle); }
}//end of class
[1] [2] 下一页 |