Heim  >  Artikel  >  Backend-Entwicklung  >  ThinkPHP标签制作教程,thinkphp标签教程_PHP教程

ThinkPHP标签制作教程,thinkphp标签教程_PHP教程

WBOY
WBOYOriginal
2016-07-13 10:23:221032Durchsuche

ThinkPHP标签制作教程,thinkphp标签教程

本文以实例讲解了ThinkPHP标签的制作方法,对于ThinkPHP的初学者或者开发人员都有一定的借鉴价值。

一般来说,ThinkPHP的默认标签解析器在Lib/Template/TagLib/TagLibCx.class(2.1版本位于ThinkPHP/Lib/Think/Template/TagLib/TagLibCx.class.php)中,里面定义了常用的如volist php 等常用ThinkPHP的标签

这里我们就在这个类中添加一个的标签解析。
 
一、标签格式:

<category parentid='0' ><{$cat.catname}></category>

二、标签作用:

循环输出父类id是parentid的栏目

三、操作步骤:

1.在tagLibCx.class 的私有属性中添加:

'category'=array('attr'=>'parentid',level=>3)

其中attr:标签的属性 level标签的嵌套层次
 
2.添加解析函数

标签的解析原理就是通过读取xml文件得到相应的信息,然后再拼凑成所需要的php源代码,最后通过echo 在页面上输出。

具体代码如下:

public function _category($attr,$content)
{
//把标签的所有属性解析到$tag数组里面
$tag = $this->parseXmlAttr($attr,'category');
//得到标签里面的属性
$parentid= $tag['parentid'];
//定义页面解析的变量
$result = !empty($tag['result'])&#63;$tag['result']:'cat'; //定义数据查询的结果存放变量
$key = !empty($tag['key'])&#63;$tag['key']:'i';
$mod = isset($tag['mod'])&#63;$tag['mod']:'2';
//拼凑数据库查询语句 这里直接用了CategoryModel封装好的函数
$sql = "D('Category')->";
$sql .= "getCategorys(".$parentid.')';
//拼凑输出字符
$parsestr = '<&#63;php $_result='.$sql.'; if ($_result): $'.$key.'=0;';
$parsestr .= 'foreach($_result as $key=>$'.$result.'):';
$parsestr .= '++$'.$key.';$mod = ($'.$key.' % '.$mod.' );&#63;>';
$parsestr .= $content;//解析在category标签中的内容
$parsestr .= '<&#63;php endforeach; endif;&#63;>';
return $parsestr;
}

CategoryModel中的getCategorys方法:

/*
* 根据parentid得到栏目信息
* $parentid 父id
* $withSelf 是否包含自己
*/
public function getCategorys($parentid,$withSelf=0)
{
$parentid=intval($parentid);
$categorys=$this->where(array('parentid'=>$parentid,'ismenu'=>1))->order('listorder ASC')->select();
//包含自己
if($withSelf)
{
$categorys2=$this->where(array('id'=>$parentid,'ismenu'=>1))->limit(1)->select();
$category=array_merge($categorys,$categorys2);
}
return $categorys;
}

3.页面上的引用:

<category parentid='0'>
<{$cat.catname}>
</category>

这样一个标签就做好了!!就可以甩掉那个volist在页面上动态输出我们想要的东西了!

用ThinkPHP制作简单的MVC-BLOG教程

官网的example就有一个完整的例子,楼主看着手册来吧例子过一遍吧
 

怎使用thinkphp框架?教程

thinkphp是一个小框架,一般你知道mvc工作模式,很快就会搞定thinkphp的
 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/840741.htmlTechArticleThinkPHP标签制作教程,thinkphp标签教程 本文以实例讲解了ThinkPHP标签的制作方法,对于ThinkPHP的初学者或者开发人员都有一定的借鉴价值。...
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn