PHP 网站模板核心类
PHP网站模板核心类
作者:Wucl(1223261610)
时间:2015-03-06
章节内容:基础背景、实际应用、个人心得(这个人非常没品德,想到什么就写什么)。
1.基础背景:
选用Blitz模板渲染页面是为了更快的渲染效率、更灵活的参数设置。
2.实际应用:
核心类设计思路:设置选项->执行功能->得到解析结果。
功能介绍(源码见附录):
1)setOption:setOption('DIR','')
DIRURL:渲染文件的文件目录路径,最后带'/',
TEMPLATE:渲染文件的文件名,前置路径为DIRURL(注意DIRURL最后带'/'),
LAYOUT:布局文件名称,前置路径为DIRURL(注意DIRURL最后带'/'),
GLOBALS:需设置的全局变量(数组),
PREFIX:调控参数(数组),把渲染文件目标参数包含的内容调到指定位置,参数名加上了blitz_前缀,
DATA:需设置的普通变量(数组),
CONTENT:布局文件内嵌的模板名称,
INCLUDEDIR:包含文件的目录,如果包含文件中带有'/'则不用此目录。
如果,输入的参数和以上所列不符,将给出提示:$name.':Theoptionparameterisundefined!'。GLOBALS、PREFIX、DATA的对应值只能是数组。
2)execute():执行函数,调用即可。
3)getParseBody():获得解析后的内容。
4)getPreBody():获得解析前的内容。
5)getOption():获得option参数内容。
实例:
/**
*渲染页面
*@paramstring$tpl
*@paramstring$layout
*/
publicfunctionrender($tpl='',$layout=''){
$this->setScriptPath();
$this->setTemplate($tpl);
$this->setVariables();
$this->initPage();
$this->setLayout($layout);
$blitz=newBaseBlitz();
$blitz->setOption('DIRURL',$this->_template_dir);//最后带有'/'
$blitz->setOption('TEMPLATE',$this->_template);
$blitz->setOption('LAYOUT',$this->_layout);
$blitz->setOption('DATA',$this->_data);
$blitz->setOption('GLOBALS',$this->_globals);
$blitz->setOption('PREFIX',$this->_prefix);
$blitz->setOption('CONTENT',$this->_content);
$blitz->setOption('INCLUDEDIR',$this->_layout_dir);
$blitz->execute();
echo$blitz->getParseBody();
exit();
}
3.个人心得:
<!--
1.$_num:从1开始$_num==1|$_num<2
2.$_odd:奇数(根据数组下标判断)
3.$_even:偶数(根据数组下标判断)
4.IF:如果TRUE者执行(IFELSEEND)
5.UNLESS:与IF相反
6.BEGIN:对数组的循环
7.url:会自动填充为当前访问地址(经过测试)
8.$_first:第一个
9.$_last:最后一个
-->
<div>
{{BEGINtplArr}}
{{IFurl}}
{{$url}}
<p>{{$id}}</p>
{{END}}
{{END}}
</div>
4.附录:
<?php
/**
*@desc重组view核心类
*@authorWucl
*@since2015-03-06
*/
namespaceLibrary\Core;
useBlitz;
classBaseBlitz{
private$_blitz;
private$_preBody;
private$_parseBody;
private$_option=array(
'DIRURL'=>'',
'TEMPLATE'=>'',
'LAYOUT'=>'',
'GLOBALS'=>array(),
'PREFIX'=>array(),
'DATA'=>array(),
'CONTENT'=>'',
'INCLUDEDIR'=>''
);
/**
*构造函数
*/
publicfunction__construct(){
$this->_blitz=newBlitz();
}
/**
*执行函数
*@return\Library\Core\BaseBlitz
*/
publicfunctionexecute(){
$this->getContent();
if(!empty($this->_preBody)){
try{
$this->_blitz->load($this->_preBody);
}catch(\Exception$e){
echo$e->getMessage();
}
}else{
echo'加载的文件不存在或为空';
}
if(!empty($this->_option['DATA'])){
$this->_blitz->set($this->_option['DATA']);
}
if(!empty($this->_option['GLOBALS'])){
$this->_blitz->setGlobals($this->_option['GLOBALS']);
}
$result=$this->getResult($this->_option['PREFIX']);
$blitzError=$this->_blitz->getError();
if(!empty($blitzError)){
error_log('blitzerror:'.$blitzError.''.$_SERVER['REMOTE_ADDR'].''.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']);
}
if(empty($result)){
error_log('blitzempty:'.$blitzError.''.$_SERVER['REMOTE_ADDR'].''.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']);
}
$this->_parseBody=$result;
return$this;
}
/**
*获取页面内容
*/
privatefunctiongetContent(){
$template=$this->_option['DIRURL'].$this->_option['TEMPLATE'];
$body=$this->_getFileContent($template);
if(!empty($this->_option['LAYOUT'])){
$layout=$this->_option['DIRURL'].$this->_option['LAYOUT'];
$part=$this->_getFileContent($layout);
//$body=preg_replace('|{{\s*'.$this->_option['CONTENT'].'*}}|U',$body,$part);
$body=preg_replace('/{{\s*\$?'.$this->_option['CONTENT'].'\s*}}/U',$body,$part);
}
if(!empty($body)){
$body=$this->_analyzeInclude($body);
}
if(!empty($body)){
//格式化内容
$searchArray=array("/<!--(?!\\[)(.*)-->/Us","/\n\n/","/\r\n\n/");
$replaceArray=array("","\n","\n");
$body=preg_replace($searchArray,$replaceArray,$body);
}
$this->_preBody=$body;
}
/**
*分析包含文件把内容读出来组合到页面中
*@param$content
*@returnmixed
*/
privatefunction_analyzeInclude($content){
//preg_match_all("|{{\s*include\s*\\(\s*(.*)\s*\\)\s*}}|U",$content,$out);
preg_match_all("/{{\s*include\s*\\(\s*(.*)\s*\\)\s*}}/U",$content,$out);
if(empty($out[0])){
return$content;
}
foreach($out[0]as$key=>$value){
if(empty($out[1][$key])){
$content=str_replace($value,'',$content);
}else{
if(preg_match('/\/|\\\/',$out[1][$key])){
$include_dir=$this->_option['DIRURL'];
}else{
$include_dir=$this->_option['DIRURL'].$this->_option['INCLUDEDIR'].DIRECTORY_SEPARATOR;
}
$fileContent=$this->_getFileContent(str_replace(array("'","\""),"",$include_dir.$out[1][$key]));
$content=str_replace($value,$fileContent,$content);
}
}
return$this->_analyzeInclude($content);
}
/**
*获取文件内容
*@paramunknown$filename
*@returnNULL|string
*/
privatefunction_getFileContent($filename){
$wholePath=str_replace('\\',DIRECTORY_SEPARATOR,$filename);
if(!file_exists($wholePath)){
echo'请给出被包含文件的正确路径!'.$wholePath."<br>";
returnnull;
}
returnfile_get_contents($wholePath);
}
/**
*获取输出内容
*@paramarray$tplGVGlobaltemplateVariable
*@returnstring
*/
publicfunctiongetResult($tplGV=array()){
if(!empty($tplGV)&&is_array($tplGV)){
foreach($tplGVas$value){
try{
if(empty($value)||is_array($value)){
thrownew\Exception($value);
}
if(!$this->_blitz->hasContext($value)){//不属于错误日志
continue;
}
$nowValue=$this->_blitz->fetch($value);
if(empty($nowValue)||is_array($nowValue)){
thrownew\Exception('nowValue 是数组:'.$nowValue);
}
$tmp['blitz_'.$value]=$nowValue;
}catch(\Exception$e){
error_log($e->getMessage());
continue;
}
}
if(!empty($tmp)&&is_array($tmp)){
$this->_blitz->setGlobals($tmp);
}
}
try{
$result=(string)$this->_blitz->parse();
}catch(\Exception$e){
error_log('blitz::parse()出错'.$_SERVER['REQUEST_URI']);
error_log('blitzdebug'.var_export($this->_blitz->getIterations(),true));
}
if(empty($result)){
error_log('blitz::parse()结果为空'.$_SERVER['REQUEST_URI']);
error_log('blitzdebug'.var_export($this->_blitz->getIterations(),true));
}
return$result;
}
/**
*设置option
*@paramunknown$name
*@paramunknown$value
*/
publicfunctionsetOption($name,$value){
$upperName=strtoupper($name);
if(!isset($this->_option[$upperName])){
exit($name.':Theoptionparameterisundefined!');
}elseif($upperName=='GLOBALS'||$upperName=='PREFIX'||$upperName=='DATA'){
if(is_array($value)){
$this->_option[$upperName]=array_merge($this->_option[$upperName],$value);
}else{
exit('setOption:Thesecondparameterisanarrayofexpectation!');
}
}else{
$this->_option[$upperName]=$value;
}
}
/**
*获取option
*@returnmultitype:stringmultitype:multitype:string
*/
publicfunctiongetOption(){
return$this->_option;
}
/**
*获取preBody
*/
publicfunctiongetPreBody(){
return$this->_preBody;
}
/**
*获取parseBody
*@returnAmbigous<string,NULL>
*/
publicfunctiongetParseBody(){
return$this->_parseBody;
}
}
?>
更多相关文章
- log4php将不同级别的日志打印到不同的日志文件中
- PHP - 在外部服务器上读取文件?
- 仅在LARAVEL或AJAX中提交表单时,无需用户身份验证即可将文件上载
- PHP ZipArchive 实现压缩解压Zip文件
- 将SQL查询的SELECT子句解析为PHP数组
- PHP不区分大小写数组搜索函数[duplicate]
- 如何找到数组的大小
- 纯php文件不加结束标记的好处
- 大文件从指定行开始读取