📄 dedetemplate.class.php
字号:
<?php
if(!defined('DEDEINC'))
{
exit("Request Error!");
}
//这个函数用于定义任意名称的块使用的接口
//返回值应是一个二维数组
function MakePublicTag($atts=array(),$refObj='',$fields=array())
{
//块调用对应的文件为 include/taglib/plus_blockname.php
/***************************************
由于标记一般存在默认属性,在编写块函数时,应该在块函数中进行给属性赋省缺值处理,如:
$attlist = "titlelen=30,catalogid=0,modelid=0,flag=,addon=,row=8,ids=,orderby=id,orderway=desc,limit=,subday=0";
//给属性赋省缺值
FillAtts($atts,$attlist);
//处理属性中使用的系统变量 var、global、field 类型(不支持多维数组)
FillFields($atts,$fields,$refObj);
****************************************/
$atts['tagname'] = ereg_replace("[0-9]{1,}$","",$atts['tagname']);
$plusfile = DEDEINC.'/tpllib/plus_'.$atts['tagname'].'.php';
if(!file_exists($plusfile))
{
if(isset($atts['rstype']) && $atts['rstype']=='string')
{
return '';
}
else
{
return array();
}
}
else
{
include_once($plusfile);
$func = 'plus_'.$atts['tagname'];
return $func($atts,$refObj,$fields);
}
}
//设定属性的默认值
function FillAtts(&$atts,$attlist)
{
$attlists = explode(',',$attlist);
foreach($attlists as $att)
{
list($k,$v)=explode('=',$att);
if(!isset($atts[$k]))
{
$atts[$k] = $v;
}
}
}
//把上级的fields传递给atts
function FillFields(&$atts,&$refObj,&$fields)
{
global $_vars;
foreach($atts as $k=>$v)
{
if(eregi('^field\.',$v))
{
$key = eregi_replace('^field\.','',$v);
if( isset($fields[$key]) )
{
$atts[$k] = $fields[$key];
}
}
else if(eregi('^var\.',$v))
{
$key = eregi_replace('^var\.','',$v);
if( isset($_vars[$key]) )
{
$atts[$k] = $_vars[$key];
}
}
else if(eregi('^global\.',$v))
{
$key = eregi_replace('^global\.','',$v);
if( isset($GLOBALS[$key]) )
{
$atts[$k] = $GLOBALS[$key];
}
}
}
}
/*------------------------
class Tag 标记的数据结构描述
function C__Tag();
-------------------------*/
class Tag
{
var $isCompiler=false; //标记是否已被替代,供解析器使用
var $tagName=""; //标记名称
var $innerText=""; //标记之间的文本
var $startPos=0; //标记起始位置
var $endPos=0; //标记结束位置
var $cAtt=""; //标记属性描述,即是class TagAttribute
var $tagValue=""; //标记的值
var $tagID = 0;
//获取标记的名称和值
function GetName()
{
return strtolower($this->tagName);
}
function GetValue()
{
return $this->tagValue;
}
function IsAtt($str)
{
return $this->cAtt->IsAttribute($str);
}
function GetAtt($str)
{
return $this->cAtt->GetAtt($str);
}
function GetinnerText()
{
return $this->innerText;
}
}
/*----------------------------
模板解析器
function C__DedeTemplate
-----------------------------*/
class DedeTemplate
{
var $tagMaxLen = 64;
var $charToLow = true;
var $isCache = true;
var $isParse = false;
var $isCompiler = true;
var $templateDir = '';
var $tempMkTime = 0;
var $cacheFile = '';
var $configFile = '';
var $buildFile = '';
var $refDir = '';
var $cacheDir = '';
var $templateFile = '';
var $sourceString = '';
var $cTags = '';
//var $definedVars = array();
var $count = -1;
var $loopNum = 0;
var $refObj = '';
var $makeLoop = 0;
var $tagStartWord = '{dede:';
var $fullTagEndWord = '{/dede:';
var $sTagEndWord = '/}';
var $tagEndWord = '}';
var $tpCfgs = array();
//构造函数
function __construct($templatedir='',$refDir='')
{
//$definedVars[] = 'var';
//缓存目录
if($templatedir=='')
{
$this->templateDir = DEDEROOT.'/templates';
}
else
{
$this->templateDir = $templatedir;
}
//模板include目录
if($refDir=='')
{
if(isset($GLOBALS['cfg_df_style']))
{
$this->refDir = $this->templateDir.'/'.$GLOBALS['cfg_df_style'].'/';
}
else
{
$this->refDir = $this->templateDir;
}
}
$this->cacheDir = DEDEROOT.$GLOBALS['cfg_tplcache_dir'];
}
function DedeTemplate($templatedir='',$refDir='')
{
$this->__construct($templatedir,$refDir);
}
//设定本类自身实例的类引用和使用本类的类实例(如果在类中使用本模板引擎,后一参数一般为$this)
function SetObject(&$refObj)
{
$this->refObj = $refObj;
}
//设定Var的键值对
function SetVar($k,$v)
{
$GLOBALS['_vars'][$k] = $v;
}
function Assign($k,$v)
{
$GLOBALS['_vars'][$k] = $v;
}
//设置标记风格
function SetTagStyle($ts='{dede:',$ftend='{/dede:',$stend='/}',$tend='}')
{
$this->tagStartWord = $ts;
$this->fullTagEndWord = $ftend;
$this->sTagEndWord = $stend;
$this->tagEndWord = $tend;
}
//获得模板设定的config值
function GetConfig($k)
{
return (isset($this->tpCfgs[$k]) ? $this->tpCfgs[$k] : '');
}
//设定模板文件
function LoadTemplate($tmpfile)
{
if(!file_exists($tmpfile))
{
echo " Template Not Found! ";
exit();
}
$tmpfile = ereg_replace("[\\/]{1,}","/",$tmpfile);
$tmpfiles = explode('/',$tmpfile);
$tmpfileOnlyName = ereg_replace("(.*)/","",$tmpfile);
$this->templateFile = $tmpfile;
$this->refDir = '';
for($i=0; $i < count($tmpfiles)-1; $i++)
{
$this->refDir .= $tmpfiles[$i].'/';
}
if(!is_dir($this->cacheDir))
{
$this->cacheDir = $this->refDir;
}
if($this->cacheDir!='')
{
$this->cacheDir = $this->cacheDir.'/';
}
if(isset($GLOBALS['_DEBUG_CACHE']))
{
$this->cacheDir = $this->refDir;
}
$this->cacheFile = $this->cacheDir.ereg_replace("\.(wml|html|htm|php)$","_".$this->GetEncodeStr($tmpfile).'.inc',$tmpfileOnlyName);
$this->configFile = $this->cacheDir.ereg_replace("\.(wml|html|htm|php)$","_".$this->GetEncodeStr($tmpfile).'_config.inc',$tmpfileOnlyName);
//不开启缓存、当缓存文件不存在、及模板为更新的文件的时候才载入模板并进行解析
if($this->isCache==false || !file_exists($this->cacheFile)
|| filemtime($this->templateFile) > filemtime($this->cacheFile))
{
$t1 = ExecTime(); //debug
$fp = fopen($this->templateFile,'r');
$this->sourceString = fread($fp,filesize($this->templateFile));
fclose($fp);
$this->ParseTemplate();
//模板解析时间
//echo ExecTime() - $t1;
}
else
{
//如果存在config文件,则载入此文件,该文件用于保存 $this->tpCfgs的内容,以供扩展用途
//模板中用{tag:config name='' value=''/}来设定该值
if(file_exists($this->configFile))
{
include($this->configFile);
}
}
}
//载入模板字符串
function LoadString($str='')
{
$this->sourceString = $str;
$hashcode = md5($this->sourceString);
$this->cacheFile = $this->cacheDir."/string_".$hashcode.".inc";
$this->configFile = $this->cacheDir."/string_".$hashcode."_config.inc";
$this->ParseTemplate();
}
//调用此函数include一个编译后的PHP文件,通常是在最后一个步骤才调用本文件
function CacheFile()
{
global $gtmpfile;
$this->WriteCache();
return $this->cacheFile;
}
//显示内容,由于函数中会重新解压一次$GLOBALS变量,所以在动态页中,应该尽量少用本方法,
//取代之是直接在程序中 include $tpl->CacheFile(),不过include $tpl->CacheFile()这种方式不能在类或函数内使用
function Display()
{
global $gtmpfile;
extract($GLOBALS, EXTR_SKIP);
$this->WriteCache();
include $this->cacheFile;
}
//保存运行后的程序为文件
function SaveTo($savefile)
{
extract($GLOBALS, EXTR_SKIP);
$this->WriteCache();
ob_start();
include $this->cacheFile;
$okstr = ob_get_contents();
ob_end_clean();
$fp = @fopen($savefile,"w") or die(" Tag Engine Create File false! ");
fwrite($fp,$okstr);
fclose($fp);
}
//解析模板并写缓存文件
function WriteCache($ctype='all')
{
if(!file_exists($this->cacheFile) || $this->isCache==false
|| ( file_exists($this->templateFile) && (filemtime($this->templateFile) > filemtime($this->cacheFile)) ) )
{
if(!$this->isParse)
{
$this->ParseTemplate();
}
$fp = fopen($this->cacheFile,'w') or dir("Write Cache File Error! ");
flock($fp,3);
fwrite($fp,trim($this->GetResult()));
fclose($fp);
if(count($this->tpCfgs) > 0)
{
$fp = fopen($this->configFile,'w') or dir("Write Config File Error! ");
flock($fp,3);
fwrite($fp,'<'.'?php'."\r\n");
foreach($this->tpCfgs as $k=>$v)
{
$v = str_replace("\"","\\\"",$v);
$v = str_replace("\$","\\\$",$v);
fwrite($fp,"\$this->tpCfgs['$k']=\"$v\";\r\n");
}
fwrite($fp,'?'.'>');
fclose($fp);
}
}
/*
if(!file_exists($this->cacheFile) || $this->isCache==false
|| ( file_exists($this->templateFile) && (filemtime($this->templateFile) > filemtime($this->cacheFile)) ) )
{
if($ctype!='config')
{
if(!$this->isParse)
{
$this->ParseTemplate();
}
$fp = fopen($this->cacheFile,'w') or dir("Write Cache File Error! ");
flock($fp,3);
fwrite($fp,trim($this->GetResult()));
fclose($fp);
}
else
{
if(count($this->tpCfgs) > 0)
{
$fp = fopen($this->configFile,'w') or dir("Write Config File Error! ");
flock($fp,3);
fwrite($fp,'<'.'?php'."\r\n");
foreach($this->tpCfgs as $k=>$v)
{
$v = str_replace("\"","\\\"",$v);
$v = str_replace("\$","\\\$",$v);
fwrite($fp,"\$this->tpCfgs['$k']=\"$v\";\r\n");
}
fwrite($fp,'?'.'>');
fclose($fp);
}
}
}
else
{
if($ctype=='config' && count($this->tpCfgs) > 0 )
{
$fp = fopen($this->configFile,'w') or dir("Write Config File Error! ");
flock($fp,3);
fwrite($fp,'<'.'?php'."\r\n");
foreach($this->tpCfgs as $k=>$v)
{
$v = str_replace("\"","\\\"",$v);
$v = str_replace("\$","\\\$",$v);
fwrite($fp,"\$this->tpCfgs['$k']=\"$v\";\r\n");
}
fwrite($fp,'?'.'>');
fclose($fp);
}
}
*/
}
//获得模板文件名的md5字符串
function GetEncodeStr($tmpfile)
{
//$tmpfiles = explode('/',$tmpfile);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -