Simple analysis of thinkphp template output function _php example

Source: Internet
Author: User

Each of the xxxAction.class.php files in the thinkphp represents an application module, and each method (function) In the action represents an operation that can be divided into operations that have output to the template and only actions that do not require output.
Opening the myapp/lib/action/indexaction.class.php file, we can see the underlying code inside:

Class Indexaction extends action{public
function Index () {
}
}

In this respect, we need to point out some points:

1. In the development of thinkphp, to add an application module, it is necessary to create a class in the Action folder, the class file name format is "module name +action.class.php." For example, our application module here is index, so the definition file name is IndexAction.class.php.
2. Apply the definition of the module class to inherit the action class of the frame. To add an action for this application module, define a function that is named after this action. For example, the index operation above.

Usually in an application module, there will be a number of operations (function) with the user to interact with the page, which requires the use of template output, thinkphp itself has built a set of thinkphp features, very powerful easy to expand but the application is very convenient and simple template engine.
In a module, if an action requires a page to be displayed, as long as it corresponds to a folder in myapp/tpl/default/, the folder is named after the name of the application module, and then in this folder, create an HTML file named after this function name, You can use the $this->display () method in this method to call the template directly. (You can, of course, call other templates under other modules or explicitly specify the location and name of the template file, because it is a step-by-step study, let us ignore it) after we understand these theories, we have a simple practice of this knowledge.
(1) Create a folder under myapp/tpl/default/, according to the name of the application module, we will name this folder index
(2) Create an HTML file under myapp/tpl/default/index/, according to the operation name, we name the file as index.html
(3) Open the myapp/lib/action/indexaction.class.php file and modify the code to

<?php
class Indexaction extends action{public
function Index () {
$value =  ' hello,thinkphp ';
$this->assign (' name ', $value);
$this->display ();
}
? >

(Excerpt from Manual: thinkphp template Guide, subsequent knowledge points are from the official thinkphp manual, no longer stated)
In the action class, use the Assign method to assign a value to a template variable, regardless of which variable type is uniformly using the Assign assignment.

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

The following wording is equivalent

$this->name = $value;

After the template variable assignment, you need to call the template file to output the related variables, and the template call is implemented by the Display method

$this->display ();


4 Open myapp/tpl/default/index/index.html file, code is

<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" >
 
 

Note here: Template variables are output using the label {$ variable name}.

Different template variable types, using different labels, the label can be defined separately, for the moment ignore.

5 Open the browser input address:

Additional Supplemental 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, you can output the name, email, and phone three variables in the template file at the same time.

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 ();

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

3 Loop This array out of the output

(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" >
 
 

Note:name= ' array ' refers to the template variable to be cycled is array,id= ' vo ' refers to the name of the data used in the 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.