欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

Dedecms之自定义标签 有大用 有大大用

shiping1 的头像

现在要做一个城市列表标签:要从城市表中获取数据并显示在页面上,系统本身不存在这样的标签,标签取名为citylist

文件部署:自定义标签的程序文件放在/include/taglib目录下

命名规范:标签程序的文件命名为[标签名称]+‘.lib.php’

即在/include/taglib目录下,新建一个文件,名为citylist.class.php

编码规范:

  1. <?php  

  2. if(!defined('DEDEINC'))  

  3. {  

  4.     exit("Request Error!");  

  5. }  

  6. function lib_demotag(&$ctag,&$refObj)  

  7. {  

  8.     global $dsql,$envs;  

  9.       

  10.     //属性处理  

  11.     $attlist="row|12,titlelen|24";  

  12.     FillAttsDefault($ctag->CAttribute->Items,$attlist);  

  13.     extract($ctag->CAttribute->Items, EXTR_SKIP);  

  14.     $revalue = '';  

  15.       

  16.     //你需编写的代码,不能用echo之类语法,把最终返回值传给$revalue  

  17.     //------------------------------------------------------  

  18.       

  19.     $revalue = 'Hello Word!';  

  20.       

  21.     //------------------------------------------------------  

  22.     return $revalue;  

  23. }  

  24. ?>  


以上代码可通过如下方式查看:后台 - 模板 - 标签源码管理 ,点击页面的右上角[增加一个新的标签]链接进入页面

函数的命名规范:lib_+标签名称,我们的标签函数名称为lib_citylist,两个参数(ctag,refObj)原封不动

具体实现:

  1. function lib_citylist(&$ctag, &$refObj)   

  2. {  

  3.     global $dsql$envs$_sys_globals;  

  4.       

  5.     ====必须的代码=====  

  6.     $attlist = "row|10";  

  7.     FillAttsDefault ( $ctag->CAttribute->Items, $attlist );  

  8.     extract ( $ctag->CAttribute->Items, EXTR_SKIP );  

  9.     ====必须的代码=====  

  10.       

  11.     ====数据获取=====  

  12.     $condtion="";  

  13.     $revalue='';  

  14.     $equery="Select id,sortid,att,attname  From tbl_city";  

  15.     if(trim($ctag->GetInnerText())==''$innertext = "[field:attname /]";  

  16.     else $innertext = $ctag->GetInnerText();  

  17.     $dsql->SetQuery($equery);  

  18.     $dsql->Execute();  

  19.     ====数据获取=====  

  20.   

  21.     ====标签替换=====  

  22.     while($dbrows=$dsql->GetObject())  

  23.     {  

  24.         $rbtext = preg_replace("/

    field:attname([\/\s]0,)

    /isU",$dbrows->attname, $innertext);  

  25.         $rbtext = preg_replace("/

    field:att([\/\s]0,)

    /isU",$dbrows->att, $rbtext);  

  26.         $revalue.=$rbtext;  

  27.     }  

  28.     $revalue = preg_replace("/

    field:tid([\/\s]0,)

    /isU",$_GET['tid'], $revalue);  

  29.     $revalue = preg_replace("/

    field:price([\/\s]0,)

    /isU",$_GET['price'], $revalue);  

  30.     $revalue = preg_replace("/

    field:is3g([\/\s]0,)

    /isU",$_GET['is3g'], $revalue);  

  31.   

  32.     ====内容返回=====  

  33.     return $revalue;  

  34. }  


在模板中的应用:

 

 

 

[html] view plaincopy 
  1. {dede:citylist row=10} --- 内容循环,属性值传递  

  2. <li><a href="list.php?tid=[field:tid /]&price=[field:price /]&citycode=[field:att /]&is3g=[field:is3g /]" >[field:attname /]</a></li>    

  3. {/dede:citylist}  

版权声明:本文为博主原创文章,未经博主允许不得转载。

来自 http://blog.csdn.net/haigou20/article/details/8618082


Dedecms之自定义标签

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

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

<?php
//检查是否定义include文件夹的路径DEDEINC<br> if(!defined('DEDEINC'))
{
    exit("Request Error!");
}
//写标签时,默认调用的方法
function lib_flink(&$ctag,&$refObj)
{
   
//声明全局的数据库连接
   global $dsql;
     //标签拥有的属性和默认值<br>   $attlist="type|textall,row|24,titlelen|24,linktype|1,typeid|0";
    FillAttsDefault($ctag->CAttribute->Items,$attlist);
 
//将标签中的值导入到当前符号表中<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>   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只是一个数值,但是却在这里能成为一个数组,求解答)<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>   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}

输出结果:

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

输出结果:

声明:本人菜鸟这几天在学PHP的Dedecms时发现能用的资料并不是很多,而且关于自定义标签的解读也很少,所以记录下来,希望出错的地方大家能提出,帮助我进步!

来自 http://www.cnblogs.com/matrix-power/archive/2011/04/11/Matrix.html




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

效果图如下所求:

helloword

 

下面我们再写一个难度大一点的自定义标签的代码如下

<?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;
}
?>

 
这样我们可以看到效果如下所示
aaaa
如果有对方法的参数&$ctag,&$refObj不是很清楚,请从index.php中的处理开始查看,具体的应该是在include目录下的Dedetag.class.php中。其实要用简单的dedecms自定义标签是完全可以参照现在的例子写出来的。


来自  http://www.seostudying.com/1511.html
 


普通分类: