Analysis of template output function of ThinkPHP

  • 2021-07-07 06:40:00
  • OfStack

Every xxxAction. class. php file in ThinkPHP represents an application module, and every method (function) in Action represents an operation, which can be divided into operations with output to templates and operations with no output.
Open the file Myapp/Lib/Action/IndexAction. class. php, and we can see the basic code inside:


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

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

1. In the development of ThinkPHP, to add an application module, it is necessary to establish a class in Action folder, and the file naming format of the class is "Module Name + Action. class. php". For example, our application module here is Index, so the definition file name is IndexAction. class. php.
2. The definition of the application module class inherits the Action class of the framework. To add an operation to this application module, define an function named after this operation. For example, the index operation above.

Usually, in an application module, there are several operations (function) that need pages to interact with users, which requires template output. ThinkPHP itself has built-in a set of template engine with ThinkPHP characteristics, which is powerful and easy to expand but very convenient and simple to apply.
In the proper module, if a certain operation requires page display, as long as a folder is established in Myapp/Tpl/default/, which is named after the application module, and then an html file named after this function is established under this folder, you can use $this- > display () method to call the template directly. (Of course, you can also call other templates under other modules or explicitly specify the location and name of template files. Because it is a step-by-step study, let's ignore it first.) After understanding these theories, we will simply practice these knowledge.
(1) Establish a folder under Myapp/Tpl/default/. According to the name of the application module, we will name this folder Index
(2) Establish an html file under Myapp/Tpl/default/Index/. According to the operation name, we name the file as index.html
(3) Open the file Myapp/Lib/Action/IndexAction. class. php and modify the code as follows


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

(Excerpted from the manual: ThinkPHP template guide, and the knowledge points since then have come from the official manual of ThinkPHP, which is no longer stated.)
In the Action class, the assign method is used to assign values to template variables, regardless of the variable type.


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

//The following writing is equivalent


$this->name = $value ;

//After assigning the template variable, it is necessary to call the template file to output the related variables, and the template call is realized by display method


$this->display();


4 Open the file Myapp/Tpl/default/Index/index. html as follows


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>{$name}</title>
</head>
<body>
 Test output:  {$name}
</body>
</html>

Note here: Template variables use the tag {$variable name} for output.

Different template variable types use different tags, which can be defined separately and ignored for the time being.

5 Open the browser and enter the address: < http://127.0.0.1/ > 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 methods:


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

In this way, three variables, name, email, and phone, can be output simultaneously in the template file.

2 We use the above variable definition to define the whole 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 Loop out this array

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


<?php
class 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:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>{$name}</title>
</head>
<body>
<iterate name="array" id="vo">
{$vo}<br />
</iterate>
</body>
</html>

Note: name= 'array' means that the template variable to be looped is array, and id= 'vo' means the name of the data to be output from the template


Related articles: