Home > PHP Framework > ThinkPHP > How to use assign() method in thinkphp

How to use assign() method in thinkphp

WBOY
Release: 2022-02-25 15:23:18
Original
5528 people have browsed it

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);".

How to use assign() method in thinkphp

The operating environment of this article: Windows 10 system, ThinkPHP version 5, Dell G3 computer.

How to use the assign() method in thinkphp

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(&#39;apple&#39;,$apple);
 
?>
Copy after login

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>
Copy after login

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(&#39;Table:apple&#39;,&#39;gbk&#39;);
Copy after login

or the template is not in HTML format, but in XML format

 $this->display(&#39;Table:apple&#39;,&#39;utf-8&#39;,&#39;text/xml&#39;);
Copy after login

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!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template