dedecms自定義標簽的方法,給出一個簡單的解析及舉例說明
首先我們來看dedecms一個現(xiàn)成的標簽flink(友情鏈接標簽),然后我們根據(jù)flink標簽自已寫一個簡單的標簽,這樣我們就很容易掌握用dedecms自定義標簽了。我們看代碼:登錄dedecms后臺=》模板=》標簽源碼管理=》flink.lib.php
里面的代碼如下:
<?php
//檢查是否定義include文件夾的路徑DEDEINC
if(!defined('DEDEINC'))
{
exit("Request Error!");
}
//寫標簽時,默認調(diào)用的方法
function
lib_flink(&
$ctag
,&
$refObj
)
{
// 聲明全局的數(shù)據(jù)庫連接
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"
;
//檢查標記中是否已經(jīng)包含innertext底層模板
if
(trim(
$ctag
->GetInnerText())==
''
)
$innertext
=
"<li>[field:link /]</li>"
;
else
$innertext
=
$ctag
->GetInnerText();
//設(shè)置查詢條件并執(zhí)行查詢
$dsql
->SetQuery(
$equery
);
$dsql
->Execute();
//通過循環(huán)獲取查詢的對象
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只是一個數(shù)值,但是卻在這里能成為一個數(shù)組,求解答)
$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;
}
?>
如果有讀者對方法的參數(shù)&$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;
}
?>
這樣我們可以看到效果如下所示
如果有對方法的參數(shù)&$ctag,&$refObj不是很清楚,請從index.php中的處理開始查看,具體的應該是在include目錄下的Dedetag.class.php中。其實要用簡單的dedecms自定義標簽是完全可以參照現(xiàn)在的例子寫出來的。
版權(quán)聲明: 本站資源均來自互聯(lián)網(wǎng)或會員發(fā)布,如果侵犯了您的權(quán)益請與我們聯(lián)系,我們將在24小時內(nèi)刪除!謝謝!
轉(zhuǎn)載請注明: Dedecms(織夢系統(tǒng))自定義標簽的方法