DEDECMS 扩展标签和dede自定义标签实现方法
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>我们需要知道下扩展标签的存放目录及文件名构成,首先,扩展的标签都是存放在/include/taglib这个目录,名称都是以“标<u>签名</u>.lib.php”格式,例如{dede:channel/}标签对应的是channel.lib.php文件。<br>
我们可以看一个示例标签:demotag.lib.php<br>
<br>
</p>
<div class="msgheader" style='margin: 3px auto 0px; padding: 0px 3px; outline: none; line-height: 21.6px; clear: both; border-width: 1px; border-style: solid; border-color: rgb(0, 153, 204); background: rgb(246, 251, 255); overflow: hidden; font-family: tahoma, arial, "Microsoft YaHei";'>
<div class="right">
<span><u>复制代码</u></span>
</div>
代码如下:</div>
<div class="msgborder" id="phpcode5" style='margin: 0px auto 3px; padding: 0px 3px; outline: none; line-height: 25.2px; font-size: 14px; clear: both; border-right: 1px solid rgb(0, 153, 204); background: rgb(221, 237, 251); overflow: hidden; border-left: 1px solid rgb(0, 153, 204); word-break: break-all; border-bottom: 1px solid rgb(0, 153, 204); word-wrap: break-word; font-family: tahoma, arial, "Microsoft YaHei";'>
<br>
if(!defined('DEDEINC'))<br>
{<br>
exit("Request Error!");<br>
}<br>
function lib_demotag(&$ctag,&$refObj)<br>
{<br>
global $dsql,$envs;<br>
//属性处理<br>
$attlist="row|12,titlelen|24";<br>
FillAttsDefault($ctag->CAttribute->Items,$attlist);<br>
extract($ctag->CAttribute->Items, EXTR_SKIP);<br>
$revalue = '';<br>
//你需编写的代码,不能用echo之类语法,把最终返回值传给$revalue<br>
//------------------------------------------------------<br>
$revalue = 'Hello Word!';<br>
//------------------------------------------------------<br>
return $revalue;<br>
}<br>
?></div>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
</p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
我们登录系统后台的[模板]-[全局标签测试]中运行{dede:demotag/},显示如下的结果:<br>
,我们会发现标签起作用了,输出了我们的内容。</p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
至此我们完成了标签的编写,这里面主要涉及到PHP、MySQL的很多知识,需要有一定相关方面的基础才能够编写标签了,当然这里只是一个简单的标签开发例子,还有许多的东西可以去开发。</p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
</p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
这里我们知道,其实标签生成的内容其实是这个函数的一个返回值,这里返回的内容都是字符串,也就是函数return $revalue;中的$revalue需要是经过处理后生成的字符串。<br>
$attlist="row|12,titlelen|24";这个是属性列表,这个经过函数处理后会直接生成变量并复制,我们可以测试下,做如下的修改:</p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
</p>
<div class="msgheader" style='margin: 3px auto 0px; padding: 0px 3px; outline: none; line-height: 21.6px; clear: both; border-width: 1px; border-style: solid; border-color: rgb(0, 153, 204); background: rgb(246, 251, 255); overflow: hidden; font-family: tahoma, arial, "Microsoft YaHei";'>
<div class="right">
<span><u>复制代码</u></span>
</div>
代码如下:</div>
<div class="msgborder" id="phpcode6" style='margin: 0px auto 3px; padding: 0px 3px; outline: none; line-height: 25.2px; font-size: 14px; clear: both; border-right: 1px solid rgb(0, 153, 204); background: rgb(221, 237, 251); overflow: hidden; border-left: 1px solid rgb(0, 153, 204); word-break: break-all; border-bottom: 1px solid rgb(0, 153, 204); word-wrap: break-word; font-family: tahoma, arial, "Microsoft YaHei";'>
<br>
$revalue = 'Hello Word!';<br>
$revalue .="<br>
Row:".$row.";TitleLen:".$titlelen;</div>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
</p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
这样我们可以看到,这个属性已经被创建变量并且赋值了。<br>
接下来我们可以再进一步去修改这个标签。<br>
比如说我们需要写一个标签专门来查询文章内容页的那个相关文章,功能类似于上面sql标签中的那个sql,只是这里我们将其分装为一个标签。<br>
我们可以新建一个标签,例如叫writerarc,那我们就需要创建一个writerarc.lib.php,然后模仿demotag编写函数,注意需要修改为<br>
function lib_writerarc(&$ctag,&$refObj)<br>
接下来我们就可以编写查询语句及对底层模板处理的相关函数了</p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
</p>
<div class="msgheader" style='margin: 3px auto 0px; padding: 0px 3px; outline: none; line-height: 21.6px; clear: both; border-width: 1px; border-style: solid; border-color: rgb(0, 153, 204); background: rgb(246, 251, 255); overflow: hidden; font-family: tahoma, arial, "Microsoft YaHei";'>
<div class="right">
<span><u>复制代码</u></span>
</div>
代码如下:</div>
<div class="msgborder" id="phpcode7" style='margin: 0px auto 3px; padding: 0px 3px; outline: none; line-height: 25.2px; font-size: 14px; clear: both; border-right: 1px solid rgb(0, 153, 204); background: rgb(221, 237, 251); overflow: hidden; border-left: 1px solid rgb(0, 153, 204); word-break: break-all; border-bottom: 1px solid rgb(0, 153, 204); word-wrap: break-word; font-family: tahoma, arial, "Microsoft YaHei";'>
<br>
$revalue = '';<br>
$innertext = $ctag->GetInnerText();<br>
$ctp = new DedeTagParse();<br>
$ctp->SetNameSpace('field', '[', ']');<br>
$sql = "SELECT * FROM dede_archives WHERE writer='{$refObj->Fields['writer']}' limit 0, $row";</div>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
</p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
$innertext这个是用来获取标签的底层模板的,$ctp创建用于处理底层模板中的变量,并处理进行替换。我们根据获取的属性编写我们的sql语句,这里我们使用limit 0, $row,这样就可以根据$row来确定查询的内容数目。<br>
当然我们可以获取更多的属性以便我们这个标签更强大,例如我们可以增加类似于arclist中的相关属性,并在函数中进行处理,不过这个需要有一定的PHP基础。<br>
接下来我们通过执行查询对sql及输出变量进行处理:</p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
</p>
<div class="msgheader" style='margin: 3px auto 0px; padding: 0px 3px; outline: none; line-height: 21.6px; clear: both; border-width: 1px; border-style: solid; border-color: rgb(0, 153, 204); background: rgb(246, 251, 255); overflow: hidden; font-family: tahoma, arial, "Microsoft YaHei";'>
<div class="right">
<span><u>复制代码</u></span>
</div>
代码如下:</div>
<div class="msgborder" id="phpcode8" style='margin: 0px auto 3px; padding: 0px 3px; outline: none; line-height: 25.2px; font-size: 14px; clear: both; border-right: 1px solid rgb(0, 153, 204); background: rgb(221, 237, 251); overflow: hidden; border-left: 1px solid rgb(0, 153, 204); word-break: break-all; border-bottom: 1px solid rgb(0, 153, 204); word-wrap: break-word; font-family: tahoma, arial, "Microsoft YaHei";'>
<br>
$dsql->Execute('me',$sql);<br>
while($rs = $dsql->GetArray('me'))<br>
{<br>
//根据属性处理查询变量<br>
$rs['title'] = cn_substr($rs['title'], $titlelen);<br>
//获取底层模板<br>
$ctp->LoadSource($innertext);<br>
foreach($ctp->CTags as $tagid=>$ctag) {<br>
if(!empty($rs)) {<br>
$ctp->Assign($tagid,$rs[$ctag->GetName()]);<br>
}<br>
}<br>
//根据底层模板及查询变量得到处理结果<br>
$revalue .= $ctp->GetResult();<br>
}</div>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
</p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
这样我们就将查询出来的结果同底层模板中出现的相关变量进行替换,然后生成输出字符串,将所有的字符串信息存储到$revalue中。<br>
最后返回这个值return $revalue;<br>
整个文件内容如下:</p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
</p>
<div class="msgheader" style='margin: 3px auto 0px; padding: 0px 3px; outline: none; line-height: 21.6px; clear: both; border-width: 1px; border-style: solid; border-color: rgb(0, 153, 204); background: rgb(246, 251, 255); overflow: hidden; font-family: tahoma, arial, "Microsoft YaHei";'>
<div class="right">
<span><u>复制代码</u></span>
</div>
代码如下:</div>
<div class="msgborder" id="phpcode9" style='margin: 0px auto 3px; padding: 0px 3px; outline: none; line-height: 25.2px; font-size: 14px; clear: both; border-right: 1px solid rgb(0, 153, 204); background: rgb(221, 237, 251); overflow: hidden; border-left: 1px solid rgb(0, 153, 204); word-break: break-all; border-bottom: 1px solid rgb(0, 153, 204); word-wrap: break-word; font-family: tahoma, arial, "Microsoft YaHei";'>
<br>
if(!defined('DEDEINC'))<br>
{<br>
exit("Request Error!");<br>
}<br>
function lib_writerarc(&$ctag,&$refObj)<br>
{<br>
global $dsql,$envs;<br>
//属性处理<br>
$attlist="row|12,titlelen|24";<br>
FillAttsDefault($ctag->CAttribute->Items,$attlist);<br>
extract($ctag->CAttribute->Items, EXTR_SKIP);<br>
$revalue = '';<br>
$innertext = $ctag->GetInnerText();<br>
$ctp = new DedeTagParse();<br>
$ctp->SetNameSpace('field', '[', ']');<br>
$sql = "SELECT * FROM dede_archives WHERE writer='{$refObj->Fields['writer']}' limit 0, $row";<br>
$dsql->Execute('me',$sql);<br>
while($rs = $dsql->GetArray('me'))<br>
{<br>
//根据属性处理查询变量<br>
$rs['title'] = cn_substr($rs['title'], $titlelen);<br>
//获取底层模板<br>
$ctp->LoadSource($innertext);<br>
foreach($ctp->CTags as $tagid=>$ctag) {<br>
if(!empty($rs)) {<br>
$ctp->Assign($tagid,$rs[$ctag->GetName()]);<br>
}<br>
}<br>
//根据底层模板及查询变量得到处理结果<br>
$revalue .= $ctp->GetResult();<br>
}<br>
return $revalue;<br>
}<br>
?><br>
接下来我们来测试我们这个标签,我们修改article_article.htm模板,在里面加入以下的标签代码:<br>
{dede:writerarc row='10' titlelen='6'}<br>
<br>
{/dede:writerarc}</div>
頁:
[1]