看了PHP和Dedecms有三天了,记录一下自定义标签的笔记。
我拿一个简单的标签flink来解释一下:
//检查是否定义include文件夹的路径DEDEINC
if(!defined('DEDEINC'))
{
exit("Request Error!");
}
//写标签时,默认调用的方法
function lib_flink(&$ctag,&$refObj)
{
//声明全局的数据库连接
global $dsql;
//标签拥有的属性和默认值
$attlist="type|textall,row|24,titlelen|24,linktype|1,typeid|0";
FillAttsDefault($ctag->CAttribute->Items,$attlist);
//将标签中的值导入到当前符号表中
extract($ctag->CAttribute->Items, EXTR_SKIP);
$totalrow = $row;
$revalue = '';
//以下为拼装SQL查询语句
$wsql = " where ischeck >= '$linktype' ";
if($typeid == 0)
{
$wsql .= '';
}
else
{
$wsql .= "And typeid = '$typeid'";
}
if($type=='image')
{
$wsql .= " And logo<>'' ";
}
else if($type=='text')
{
$wsql .= " And logo='' ";
}
$equery = "Select * from dede_flink $wsql order by sortrank asc limit 0,$totalrow";
//检查标记中是否已经包含innertext底层模板
if(trim($ctag->GetInnerText())=='') $innertext = "
else $innertext = $ctag->GetInnerText();
//设置查询条件并执行查询
$dsql->SetQuery($equery);
$dsql->Execute();
//通过循环获取查询的对象
while($dbrow=$dsql->GetObject())
{
if($type=='text'||$type=='textall')
{
$link = "".cn_substr($dbrow->webname,$titlelen)." ";
}
else if($type=='image')
{
}
else
{
if($dbrow->logo=='')
{
$link = "".cn_substr($dbrow->webname,$titlelen)." ";
}
else
{
}
}
//对innertext中的字符串进行值替换(暂时不清楚为什么标签里面的row只是一个数值,但是却在这里能成为一个数组,求解答)
$rbtext = preg_replace("/[field:url([/s]{0,})]/isU", $row['url'], $innertext);
$rbtext = preg_replace("/[field:webname([/s]{0,})]/isU", $row['webname'], $rbtext);
$rbtext = preg_replace("/[field:logo([/s]{0,})]/isU", $row['logo'], $rbtext);
$rbtext = preg_replace("/[field:link([/s]{0,})]/isU", $link, $rbtext);
$revalue .= $rbtext;
}
//返回替换处理好的字符串
return $revalue;
}
?>
如果有读者对方法的参数&$ctag,&$refObj不是很清楚,请从index.php中的处理开始查看,具体的应该是在include目录下的Dedetag.class.php中。
下面,我们仿照上面的例子自己去写一个标签
if (! defined ( 'DEDEINC' )) {
exit ( "Request Error!" );
}
function lib_aaa(&$ctag, &$refObj)
{
global $dsql;
$attlist = "topid|0,row|10";
FillAttsDefault ( $ctag->CAttribute->Items, $attlist );
extract ( $ctag->CAttribute->Items, EXTR_SKIP );
$condtion="";
$revalue='';
if($topid==0)
{
$condtion.=" where topid=0";
}
elseif ($topid!=0)
{
$condtion.=" where topid <> 0";
}
$equery="select * from `dede_arctype` $condtion";
if(trim($ctag->GetInnerText())=='') $innertext = "
else $innertext = $ctag->GetInnerText();
$dsql->SetQuery($equery);
$dsql->Execute();
while($dbrows=$dsql->GetObject())
{
$rbtext = preg_replace("/[field:typename([/s]{0,})]/isU",$dbrows->typename, $innertext);
$revalue.=$rbtext;
}
return $revalue;
}
?>
上面的代码比较简单,在这里就不做解释了,直接看测试结果:
//这个测试不包含innertext,默认输出应该是
{/dede:aaa}
输出结果:
//自带的innertext,应该输出是按照加粗+分割线格式
{dede:aaa topid='0'row=10}
[field:typename /]
{/dede:aaa}
输出结果:
声明:本人菜鸟这几天在学PHP的Dedecms时发现能用的资料并不是很多,而且关于自定义标签的解读也很少,所以记录下来,希望出错的地方大家能提出,帮助我进步!