Analysis of ThinkPHP template output function

Source: Internet
Author: User
This article mainly introduces the ThinkPHP template output function. For more information, see ThinkPHP.

Every xxxAction in ThinkPHP. class. the PHP file represents an application module. each function in this Action represents an operation. operations can be divided into operations that output to a template and only operations that do not need to be output.
Open the Myapp/Lib/Action/IndexAction. class. php file, and we can see the basic code:

class IndexAction extends Action{public function index(){}}

In this regard, you need to point out the following points:

1. in ThinkPHP development, to add an application module, you must create a class in the Action folder. the file naming format of the class is "module name + Action. class. php ". For example, the application module here is Index, so the file name is defined as IndexAction. class. php.
2. define the application module class to inherit the Action class of the framework. To add an operation for this application module, define a function named after this operation. for example, the index operation above.

In an application module, several operations (functions) need pages that interact with users. Therefore, the template output is required. ThinkPHP has a set of built-in ThinkPHP features, it is very easy to expand, but the application is very convenient and simple template engine.
In the proper module, if an operation requires page display, as long as a folder is created in Myapp/Tpl/default/, the folder is named by the name of the application module, then, create an html file named after this function in this folder, and you can use the $ this-> display () method in this method to directly call this template. (Of course, you can also call other templates in other modules or explicitly specify the location and name of the template file. because it is a step-by-step learning, let's ignore it first.) after understanding these theories, let's take a look at this knowledge.
(1) create a folder under Myapp/Tpl/default/. based on the application module name, name this folder as Index.
(2) create an HTML file under Myapp/Tpl/default/Index/. The operation name is "index.html ".
(3) open the Myapp/Lib/Action/IndexAction. class. php file and modify the code

<?phpclass IndexAction extends Action{public function index(){$value =  'hello,ThinkPHP';$this->assign('name',$value);$this->display();}}?>

(From the manual: ThinkPHP template guide. the subsequent knowledge points are from the ThinkPHP official manual and will not be stated)
Use the assign method in the Action class to assign values to template variables. all variable types use assign for assignment.

$this->assign('name',$value);

// The following statement is equivalent.

$this->name = $value ;

// After the template variable value is assigned, you need to call the template file to output the relevant variables. the template call is implemented through the display method.

$this->display();


4. open the Myapp/Tpl/default/Index/index.html file. the code is

{$ Name}Test output: {$ name}

Note: The Template variables are output using the tag {$ variable name.

For different template variable types, different tags can be defined separately.

5. open your browser and enter the address: , We can see that the defined Template variables have been output.

Additional supplementary knowledge:

1 If you want to output multiple Template variables at the same time, you can use the following method:

$array = array();$array['name']  =  'thinkphp';$array['email']  =  '123456@vip.qq.com';$array['phone']  =  '123456';$this->assign($array);

In this way, you can output the name, email, and phone variables in the template file at the same time.

2. use the above variable definition to define the entire array as a template variable to output

$array = array();$array['name']  =  'thinkphp';$array['email']  =  '123456@vip.qq.com';$array['phone']  =  '123456';$this->assign('array',$array);$this->display();

In html, to output the value of $ array ['name'], the code is
{$ Array. name} or {$ array ['name']}

3. output this array cyclically

(1) code changes in IndexAction. class. php are as follows:

<?phpclass IndexAction extends Action{public function index(){$array = array();$array['name']  =  'thinkphp';$array['email']  =  '123456@vip.qq.com;$array['phone']  =  '123456';$value =  'hello,ThinkPHP';$this->assign('array',$array);$this->assign('name',$value);$this->display();}}?>

(2) change the Myapp/Tpl/default/Index/index.html code as follows:

{$name}
 
  {$vo}
  

Note:Name = 'array' indicates that the template variable to be cyclic is array, and id = 'vo' indicates the name used by the data for template output.

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.