Home Backend Development PHP Tutorial ThinkPHP标签制作教程_php实例

ThinkPHP标签制作教程_php实例

Jun 07, 2016 pm 05:17 PM
thinkphp make Label

本文以实例讲解了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>

Copy after login

二、标签作用:

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

三、操作步骤:

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

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

Copy after login

其中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;
}
Copy after login

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

Copy after login

3.页面上的引用:

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

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

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to run thinkphp project How to run thinkphp project Apr 09, 2024 pm 05:33 PM

To run the ThinkPHP project, you need to: install Composer; use Composer to create the project; enter the project directory and execute php bin/console serve; visit http://localhost:8000 to view the welcome page.

There are several versions of thinkphp There are several versions of thinkphp Apr 09, 2024 pm 06:09 PM

ThinkPHP has multiple versions designed for different PHP versions. Major versions include 3.2, 5.0, 5.1, and 6.0, while minor versions are used to fix bugs and provide new features. The latest stable version is ThinkPHP 6.0.16. When choosing a version, consider the PHP version, feature requirements, and community support. It is recommended to use the latest stable version for best performance and support.

How to run thinkphp How to run thinkphp Apr 09, 2024 pm 05:39 PM

Steps to run ThinkPHP Framework locally: Download and unzip ThinkPHP Framework to a local directory. Create a virtual host (optional) pointing to the ThinkPHP root directory. Configure database connection parameters. Start the web server. Initialize the ThinkPHP application. Access the ThinkPHP application URL and run it.

How to search for text across all tabs in Chrome and Edge How to search for text across all tabs in Chrome and Edge Feb 19, 2024 am 11:30 AM

This tutorial shows you how to find specific text or phrases on all open tabs in Chrome or Edge on Windows. Is there a way to do a text search on all open tabs in Chrome? Yes, you can use a free external web extension in Chrome to perform text searches on all open tabs without having to switch tabs manually. Some extensions like TabSearch and Ctrl-FPlus can help you achieve this easily. How to search text across all tabs in Google Chrome? Ctrl-FPlus is a free extension that makes it easy for users to search for a specific word, phrase or text across all tabs of their browser window. This expansion

Specific method to create film movement effect in PPT Specific method to create film movement effect in PPT Mar 26, 2024 pm 04:00 PM

1. Start PPT, create a new blank document, select all text boxes and delete them. 2. Execute the Insert-Shape command, drag a rectangle in the document, and fill the shape with black. 3. Drag the rectangle to elongate it, execute the Insert-Shape command, drag out the small square, and set the fill color to white. 4. Copy and paste the small squares one by one so that the top and bottom are evenly distributed on both sides of the film. After selecting them all with ctrl+a, right-click and select Group. 5. Execute the Insert-Picture command, find the picture to be inserted in the pop-up dialog box, click to open, and adjust the size and position of the picture. 6. Repeat step 5 to insert and set the remaining pictures in order to form a film picture. 7. Select the film, execute animation-add animation command

Which one is better, laravel or thinkphp? Which one is better, laravel or thinkphp? Apr 09, 2024 pm 03:18 PM

Performance comparison of Laravel and ThinkPHP frameworks: ThinkPHP generally performs better than Laravel, focusing on optimization and caching. Laravel performs well, but for complex applications, ThinkPHP may be a better fit.

How to make a tomato novel cover How to make a tomato novel cover Feb 23, 2024 pm 01:55 PM

How to make the cover of Tomato novel? You can make exclusive novel cover in Tomato novel, but most friends don’t know how to make the cover of Tomato novel. Next is the picture of how to make the cover of Tomato novel brought by the editor to the players. Tutorial, interested players come and take a look! Tomato Novel usage tutorial How to make a Tomato Novel cover 1. First open the Tomato Novel APP, enter the work management page to create a new book, and select the [Cover Template] as shown by the arrow below; 2. Then enter the cover template page and select your favorite cover Template; 3. After finally selecting the cover, click [Confirm] in the upper right corner.

Operation guide for creating mobile Excel tables Operation guide for creating mobile Excel tables Feb 18, 2024 pm 02:41 PM

Mobile Excel table creation tutorial With the popularity of mobile devices and the continuous advancement of technology, mobile phones have become one of the indispensable tools in our daily life and work. Using Excel spreadsheets on your mobile phone can easily record, calculate and analyze data and improve work efficiency. This article will share with you the basic operations and techniques for creating mobile Excel tables. 1. Choose the right application. There are many mobile Excel applications on the market to choose from, such as GoogleSheets, Micro

See all articles