dedecms 自定义标签

原文地址:dedecms 自定义标签 作者:落花有意

Dedecms之自定义标签

看了PHP和Dedecms有三天了,记录一下自定义标签的笔记。

我拿一个简单的标签flink来解释一下:

<?php<BR> //检查是否定义include文件夹的路径DEDEINC<BR> if(!defined('DEDEINC'))
{
     exit ( "Request Error!" );
}
//写标签时,默认调用的方法
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);
  
     $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 #@__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> " ;
         }
         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> " ;
             }
         }<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 );
         $revalue .= $rbtext ;
     }<BR>    //返回替换处理好的字符串<BR>   return $revalue;
}
?>

如果有读者对方法的参数&$ctag,&$refObj不是很清楚,请从index.php中的处理开始查看,具体的应该是在include目录下的Dedetag.class.php中。

下面,我们仿照上面的例子自己去写一个标签

<?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 `#@__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 ;
     }
?>

上面的代码比较简单,在这里就不做解释了,直接看测试结果:

//这个测试不包含innertext,默认输出应该是<li><BR>{dede:aaa topid='0'row=10}
{/dede:aaa}

输出结果:

[转载]dedecms <wbr>自定义标签

//自带的innertext,应该输出是按照加粗+分割线格式
{dede:aaa topid= '0' row=10}
<b>[field:typename /]</b><hr />
{/dede:aaa}

输出结果:

[转载]dedecms <wbr>自定义标签

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值