A brief analysis of ThinkPHP's template output function_PHP tutorial

WBOY
Release: 2016-07-13 10:26:31
Original
711 people have browsed it

Each xxxAction.class.php file in ThinkPHP represents an application module. Each method (function) in this Action represents an operation. Operations can be divided into operations that output to the template and operations that only execute. Operations that do not require output.
Open the Myapp/Lib/Action/IndexAction.class.php file and we can see the basic code inside:

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

Copy after login

In this regard, a few points need to be pointed out:

1. In the development of ThinkPHP, if you want 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, our application module here is Index, so the definition file is named IndexAction.class.php.
2. The definition of the application module class must inherit the Action class of the framework. To add an operation to this application module, define a function named after this operation. For example, the index operation above.

Usually in an application module, there will be several operations (functions) that require pages to interact with the user, which requires the use of template output. ThinkPHP itself has built-in a set of ThinkPHP features, which are very powerful and easy to expand but can be used. Very convenient and simple template engine.
In the due module, if a certain operation requires page display, just create a corresponding folder in Myapp/Tpl/default/. The folder is named after the name of the application module, and then create a folder under this folder. For an html file named with this function name, you can use the $this->display() method in this method to directly call the template. (Of course, you can also call other templates under other modules or explicitly specify the template file location and name. Since it is a step-by-step learning, let us ignore it for now.) After understanding these theories, let's briefly practice this knowledge.
(1) Create a folder under Myapp/Tpl/default/. According to the name of the application module, we name this folder Index
(2) Create an html file under Myapp/Tpl/default/Index/. According to the operation name, we name the file index.html
(3) Open the Myapp/Lib/Action/IndexAction.class.php file and modify the code to

<&#63;php
class IndexAction extends Action{
public function index(){
$value =  'hello,ThinkPHP';
$this->assign('name',$value);
$this->display();
}
}
&#63;>
Copy after login

(Excerpted from the manual: ThinkPHP Template Guide, the following knowledge points are all from the official ThinkPHP manual, no further statement)
Use the assign method in the Action class to assign values ​​to template variables. Regardless of the variable type, assign assignment is used uniformly.

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

// The following writing methods are equivalent

$this->name = $value ;
Copy after login

// After the template variable is assigned a value, the template file needs to be called to output the relevant variables. The template call is implemented through the display method

$this->display();
Copy after login


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

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>{$name}</title>
</head>
<body>
测试输出: {$name}
</body>
</html>
Copy after login

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

Different template variable types use different labels. The labels can be defined separately, so ignore them for now.

5 Open the 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);
Copy after login

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

2 We 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();
Copy after login

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

3 Loop this array and output it

(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();
}
}
?>
Copy after login

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

Note: name='array' means that the template variable to be looped is array, id='vo' refers to the name used for this data when outputting the template

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/824675.htmlTechArticleEach xxxAction.class.php file in ThinkPHP represents an application module, and each action in this Action A method (function) represents an operation, and the operation can be divided into output to the template...
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!