Brief analysis of thinkphp template output function _php example

Source: Internet
Author: User
Each xxxAction.class.php file in the thinkphp represents an application module in which each method (function) represents an operation that can be divided into operations that have output to the template and operations that do not require output.
Open the myapp/lib/action/indexaction.class.php file and we can see the underlying code inside:

Class Indexaction extends Action{public function index () {}}

In this respect, we need to point out a few points:

1. In the development of thinkphp, in order to add an application module, it is necessary to 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. Apply the definition of the module class to inherit the framework's action class. To add an action for this application module, define a function that is named for this action. For example, the index operation above.

Usually in an application module, there will be a number of operations (function) need to interact with the user page, which requires the use of template output, thinkphp itself has built a set of thinkphp features, very powerful and easy to expand, but the application is very convenient and simple template engine.
In the due module, if an action is required to display the page, as long as the corresponding in the myapp/tpl/default/to create a folder, the folder is named after the application module name, and then in this folder, create a name of the function named HTML file, You can use the $this->display () method in this method to invoke the template directly. (Of course, you can also call other templates under other modules or explicitly specify the template file location and name, because it is a gradual learning, let us first ignore it) after the understanding of these theories, we first simple to practice 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, modify the code to

<?phpclass Indexaction extends Action{public function index () {$value =  ' hello,thinkphp '; $this->assign (' Name ', $value); $this->display ();}}? >

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

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

The following notation is equivalent to the

$this->name = $value;

After assigning a template variable, you need to invoke the template file to output the associated variable, and the template call is implemented by the Display method

$this->display ();


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

{$name}Test output: {$name}

Note here: The template variable uses the {$ variable name} to output the label.

Different template variable types, using different tags, labels can be defined by themselves, for the moment ignore.

5 Open the browser input address : As we can see, the defined template variable has been output.

Additional Supplemental 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);

This allows you to simultaneously output name, email, and phone three variables 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 ();

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

3 Loop the array out

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

<?phpclass 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:

{$name}
 
  
   
  {$VO}
  
   

Note:name= ' array ' means that the template variable to be looped is array,id= ' vo ' refers to the name used by this data 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.