Analysis on the principle of transferring ThinkPHP variables to the template

Source: Internet
Author: User
ThinkPHP PHP variables should be passed to the template principles analysis programmers should be standing on the shoulders of predecessors, so Analysis of excellent open-source products and frameworks I think it is a programmer must do things. Recently, I tried to write a program independently and used the php template in the background. Simply put, the template uses the original PHP code for loop, and the ThinkPHP PHP variable is passed to the template.

Programmers should be standing on the shoulders of their predecessors. Therefore, analyzing excellent open-source products and frameworks is something programmers must do.

Recently, I tried to write a program independently and use the php template in the background. in simple words, the template uses the original PHP code for loop and display content.

$this->display();


I am familiar with thinkphp, which is a template reference code. This is what I wrote at the beginning.

$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();

In the template, an error is reported, indicating that setting is not found. Unfortunately, because the template reference is in display, the variables declared here cannot be included. they are all class methods. what is the problem, broken eggs.

At first, I thought about global to implement super-global transfer. but think about it, it seems that the efficiency is not high, and every referenced file is written like this. the more you think about it, the more you write, is difficult to maintain and read. You must let yourself and readers know that this variable has been referenced in the template, so I keep my eyes on it.

$this->assign()

The general process is as follows:

1. declare a class variable whose initial value is array ()

protected $_var = array();

2. use assign to pass local variables to class variables

$this->_var[$name] = $value;

3. in the method display, use the extract function to break $ this-> _ var into local variables of the current method, and then apply the template in the display.

include $tplfile;

Finally:

$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();


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.