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