Home Backend Development PHP Tutorial ThinkPHP的PHP变量传送到模板原理浅析

ThinkPHP的PHP变量传送到模板原理浅析

Jun 13, 2016 am 11:15 AM
display list this

ThinkPHP的PHP变量传递到模板原理浅析

程序员就应该是站在前人的肩膀上前进,所以,分析优秀的开源产品以及框架我觉得是程序员必做事情。

最近在尝试自己独立编写一个程序,在后台用到了php模板,简单来说,就是模板中用原生态的PHP代码做循环、显示内容。

$this->display();
Copy after login


这个对于使用thinkphp的朋友应该是很熟悉了,就是一个引用模板的代码,我也是这么做的。开始的时候,我是这么写的

		$list = $this->query('select * from %pre%yiciconf where left(`key`,4)=\'sys_\'');		$setting = array();		if (!empty($list)) {			foreach ($list as $value){				$setting[$value['key']] = $value['value'];			}		}				$this->display();
Copy after login

模板中居然报错,说找不到setting,悲剧啊,因为模板的引用在display中,所以这里声明的变量根本没法进去,都是类方法,这是闹哪样,蛋碎。

开始也想过global来进行超全局传递,但是想想,貌似效率不高,而且每个引用的都这么写,想想越写越多,都是难以维护以及阅读的事情。必须让自己以及阅读者知道这个变量引用到模板中了,于是我眼睛紧盯

$this->assign()
Copy after login

总体来说,大致流程如下:

1、声明一个初始值为array()的类变量

protected $_var = array();
Copy after login

2、使用方法assign将局部变量传递到类变量中

$this->_var[$name] = $value;
Copy after login

3、在方法display中,使用extract函数将$this->_var分解成当前方法的局部变量,然后在display中应用模板

include $tplfile;
Copy after login

最后:

		$list = $this->query('select * from %pre%yiciconf where left(`key`,4)=\'sys_\'');		$setting = array();		if (!empty($list)) {			foreach ($list as $value){				$setting[$value['key']] = $value['value'];			}		}		$this->assign('setting', $setting);		$this->display();
Copy after login


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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 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)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1269
29
C# Tutorial
1248
24
Fujifilm X-M5 price expectations rise as leaked selfie display adds complexity to the formerly affordable camera line Fujifilm X-M5 price expectations rise as leaked selfie display adds complexity to the formerly affordable camera line Sep 07, 2024 am 09:34 AM

Fujifilm fans were recently very excited at the prospect of the X-T50, since it presented a relaunch of the budget-oriented Fujifilm X-T30 II that had become quite popular in the sub-$1,000 APS-C category. Unfortunately, as the Fujifilm X-T50's launc

Apple Studio Display's power cord is detachable, but requires special tools Apple Studio Display's power cord is detachable, but requires special tools May 17, 2023 pm 03:05 PM

Apple Studio Display is now officially available in stores, and many customers around the world have purchased the product. Unlike the ProDisplayXDR, the StudioDisplay has a unique power connector that appears to be non-removable. It turns out that the cable is removable, but you'll need a special tool to remove it. Apple says on its website that the Studio Display's power cord is not detachable -- and many users think so. That's because removing the cable with your hands seems impossible, but luckily the cable can be detached from the monitor. , Apple has a special tool for extracting information from its new StudioDispl

Comparison: Apple Studio Display vs Samsung Smart Monitor M8 Comparison: Apple Studio Display vs Samsung Smart Monitor M8 May 11, 2023 pm 10:46 PM

Samsung Smart Display M8 vs. Apple Studio Display: Design and Size Since its launch, the Apple Studio Display has been compared to the iMac, consisting of a relatively thin panel on a relatively simple L-shaped stand. It's a well-known and well-loved aesthetic, and Samsung seems to have borrowed it for its presentation. The Samsung SmartMonitor M8 uses the same idea of ​​a thin screen on a stand that looks very similar. Some minor elements are different, such as the small section in the lower left corner that sticks out a bit and the Samsung's chin is very thin, but they seem to be close in terms of basic design. Samsung seems to have taken a lot of inspiration from the 24-inch iMac. Apple's display is smaller than Samsung's

display what does it mean display what does it mean Oct 26, 2023 am 11:50 AM

Display usually refers to the operation or function of displaying data, information or results to the user in some way or outputting it to a screen or other device. Specific meaning: 1. In the command line interface (CLI), display may refer to outputting data in text, tables or other formats to the terminal window for users to view or analyze; 2. In the graphical user interface (GUI), Display may refer to displaying images, text, charts and other content on the application window or interface for user interaction or browsing, etc.

Boot Camp updated to support Apple Studio Display Boot Camp updated to support Apple Studio Display May 20, 2023 pm 11:34 PM

IntelMac users running Windows on a Mac can now update their drivers in BootCamp to support Apple's StudioDisplay. Apple regularly updates BootCamp to introduce support for new hardware, as well as typical compatibility and performance improvements. In the March software update, Apple has enabled BootCamp to work with the new StudioDisplay. The update that brings BootCamp to version 6.1.17 introduces two key support elements. First, it adds compatibility with StudioDisplay, ensuring

What values ​​does display have? What values ​​does display have? Nov 20, 2023 pm 05:28 PM

The values ​​of display include block, inline, none, inline-block, flex, grid, table, inline-table and list-item. Detailed introduction: 1. block, which renders elements into block-level elements. Block-level elements form a block on the page and occupy one line alone; 2. inline, which renders elements into inline elements. Inline elements will not occupy a line by themselves and can be side by side with other elements; 3. none, this value specifies that the element will not be on the page, etc.

Google Pixel 9 Pro XL ranks 2nd in DxOMark\'s \'Global\' smartphone camera ranking Google Pixel 9 Pro XL ranks 2nd in DxOMark\'s \'Global\' smartphone camera ranking Aug 23, 2024 am 06:42 AM

Google's new Pixel 9 series has introduced a new variant that wasn't present in previous lineups, the Pixel 9 Pro XL. It is essentially identical to the non-XL variant (pre-order at Amazon), but as the name suggests, it has a larger screen. The two e

Google Pixel 8 display problems: Repair program enables 3 years of free service Google Pixel 8 display problems: Repair program enables 3 years of free service Jun 15, 2024 pm 06:50 PM

SincethelaunchoftheGooglePixel8(approx.513eurosatAmazon,limited-timedeal),therehavebeenanincreasingnumberofreportsfromuserscomplainingaboutaverticallineonthedisplay.Thiscanmanifestitselfindifferentways-thecolorand

See all articles