dedecms自定义标签的方法,给出一个简单的解析及举例说明
首先我们来看dedecms一个现成的标签flink(友情链接标签),然后我们根据flink标签自已写一个简单的标签,这样我们就很容易掌握用dedecms自定义标签了。我们看代码:登录dedecms后台=》模板=》标签源码管理=》flink.lib.php
里面的代码如下:
<?php
//检查是否定义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
=
"<li>[field:link /]</li>"
;
else
$innertext
=
$ctag
->GetInnerText();
//设置查询条件并执行查询
$dsql
->SetQuery(
$equery
);
$dsql
->Execute();
//通过循环获取查询的对象
while($dbrow=$dsql->GetObject())
{
if
(
$type
==
'text'
||
$type
==
'textall'
)
{
$link
=
"<a href='"
.
$dbrow
->url.
"' target='_blank'>"
.cn_substr(
$dbrow
->webname,
$titlelen
).
"</a> "
;
}
else
if
(
$type
==
'image'
)
{
$link
=
"<a href='"
.
$dbrow
->url.
"' target='_blank'><img src='"
.
$dbrow
->logo.
"' width='88' height='31' border='0'></a> "
;
}
else
{
if
(
$dbrow
->logo==
''
)
{
$link
=
"<a href='"
.
$dbrow
->url.
"' target='_blank'>"
.cn_substr(
$dbrow
->webname,
$titlelen
).
"</a> "
;
}
else
{
$link
=
"<a href='"
.
$dbrow
->url.
"' target='_blank'><img src='"
.
$dbrow
->logo.
"' width='88' height='31' border='0'></a> "
;
}
}
// 对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;
}
?>
下面,我们仿照上面的例子自己去写一个标签
<?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 = "<li>[field:typename /]</li>";
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;
}
?>
如果有读者对方法的参数&$ctag,&$refObj不是很清楚,请从index.php中的处理开始查看,具体的应该是在include目录下的Dedetag.class.php中,下面我们来写一个最简单的helloword标签
<?php
if(!defined('DEDEINC'))
{
exit("Request Error!");
}
function lib_demotag(&$ctag,&$refObj)
{
global $dsql,$envs;
//属性处理
$attlist="row|12,titlelen|24";
FillAttsDefault($ctag->CAttribute->Items,$attlist);
extract($ctag->CAttribute->Items, EXTR_SKIP);
$revalue = '';
//你需编写的代码,不能用echo之类语法,把最终返回值传给$revalue
//------------------------------------------------------
$revalue = 'Hello Word!';
//------------------------------------------------------
return $revalue;
}
?>
效果图如下所求:
下面我们再写一个难度大一点的自定义标签的代码如下
<?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 = "<li>[field:typename /]</li>";
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;
}
?>
这样我们可以看到效果如下所示
如果有对方法的参数&$ctag,&$refObj不是很清楚,请从index.php中的处理开始查看,具体的应该是在include目录下的Dedetag.class.php中。其实要用简单的dedecms自定义标签是完全可以参照现在的例子写出来的。
版权声明: 本站资源均来自互联网或会员发布,如果侵犯了您的权益请与我们联系,我们将在24小时内删除!谢谢!
转载请注明: Dedecms(织梦系统)自定义标签的方法