suchen
Heimphp教程php手册ThinkPHP标签制作教程

这篇文章主要介绍了ThinkPHP标签制作方法,需要的朋友可以参考下

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

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

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

二、标签作用:

循环输出父类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'])?$tag['result']:'cat'; //定义数据查询的结果存放变量 $key = !empty($tag['key'])?$tag['key']:'i'; $mod = isset($tag['mod'])?$tag['mod']:'2'; //拼凑数据库查询语句 这里直接用了CategoryModel封装好的函数 $sql = "D('Category')->"; $sql .= "getCategorys(".$parentid.')'; //拼凑输出字符 $parsestr = '$'.$result.'):'; $parsestr .= '++$'.$key.';$mod = ($'.$key.' % '.$mod.' );?>'; $parsestr .= $content;//解析在category标签中的内容 $parsestr .= ''; 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.页面上的引用:

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

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

Heiße KI -Werkzeuge

Undresser.AI Undress

Undresser.AI Undress

KI-gestützte App zum Erstellen realistischer Aktfotos

AI Clothes Remover

AI Clothes Remover

Online-KI-Tool zum Entfernen von Kleidung aus Fotos.

Undress AI Tool

Undress AI Tool

Ausziehbilder kostenlos

Clothoff.io

Clothoff.io

KI-Kleiderentferner

AI Hentai Generator

AI Hentai Generator

Erstellen Sie kostenlos Ai Hentai.

Heißer Artikel

R.E.P.O. Energiekristalle erklärten und was sie tun (gelber Kristall)
3 Wochen vorBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Beste grafische Einstellungen
3 Wochen vorBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. So reparieren Sie Audio, wenn Sie niemanden hören können
3 Wochen vorBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: Wie man alles in Myrise freischaltet
4 Wochen vorBy尊渡假赌尊渡假赌尊渡假赌

Heiße Werkzeuge

SAP NetWeaver Server-Adapter für Eclipse

SAP NetWeaver Server-Adapter für Eclipse

Integrieren Sie Eclipse mit dem SAP NetWeaver-Anwendungsserver.

Herunterladen der Mac-Version des Atom-Editors

Herunterladen der Mac-Version des Atom-Editors

Der beliebteste Open-Source-Editor

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Leistungsstarke integrierte PHP-Entwicklungsumgebung

VSCode Windows 64-Bit-Download

VSCode Windows 64-Bit-Download

Ein kostenloser und leistungsstarker IDE-Editor von Microsoft

Senden Sie Studio 13.0.1

Senden Sie Studio 13.0.1

Leistungsstarke integrierte PHP-Entwicklungsumgebung