In thinkphp, the assign() method is used to print an array. The first parameter of this method is the variable name used when getting the value of the template. The second parameter is the value to be passed. The syntax is: "$this->assign('name',$value);".
The operating environment of this article: Windows 10 system, ThinkPHP version 5, Dell G3 computer.
Let’s talk about $this->assign() first.
Its function is very simple, it is to print out the array.
For example:
<?php $apple = "苹果"; $this->assign('apple',$apple); ?>
The first parameter in assign is the variable name used when the template gets the value, and the second parameter is the value to be passed.
<html> <head> <title>取值</title> </head> <body> {$apple} </body> </html>
This way you can pass the value into the template.
Let’s talk about $this->display()
Its function is to put the typed data into the corresponding template. Normally there is no need to assign a value here, because it will automatically find the corresponding template file according to the naming rules. But there are always some other situations, so let’s talk about other situations.
1. Call other templates of the current module
Format: $this->display('template name');
For example: Assume that the current operation is the Table module Under apple, we need to call orange under the Table module.
You can write $this->display('orange') like this.
2. Call other modules’ operations
Format: $this->display('Module name: template name')
For example: Assume that the current operation is Table module. We need to call pizza from the Food module.
Just write $this->display('Food:pizza').
3. Full path output template
Format: $this->display('path file name');
For example: we output the current public directory add.html
You can write $this->display('./public/add.html'); like this.
4. Other parameter methods
We want to specify the encoding of template output instead of the default encoding.
You can use
$this->display('Table:apple','gbk');
or the template is not in HTML format, but in XML format
$this->display('Table:apple','utf-8','text/xml');
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to use assign() method in thinkphp. For more information, please follow other related articles on the PHP Chinese website!