Analysis of variable output of thinkphp template engine

Source: Internet
Author: User
This article mainly introduces the variable output usage of the thinkphp template engine, analyzes the common usage and usage skills of the variable output, and is very useful, and the friends who need can refer to the following

This paper analyzes the usage of variable output in thinkphp template engine. Share to everyone for your reference. The specific analysis is as follows:

We already know how to use the Assign method in action to assign a value to a template variable, and how to output the value of a variable in the template file after the assignment?

If we assign a name template variable in action:

$name = ' thinkphp '; $this->assign (' name ', $name);

Using the built-in template engine output variable, you only need to use it in the stencil file:
{$name}

The result of the template compilation is

<?php Echo ($name);? >

At the end of the run, you can display the output of the thinkphp in the label position, noting that the template label cannot have any spaces between {and $, otherwise the label is invalid. The normal tag default start tag is {, the end tag is}, or you can make changes by setting Tmpl_l_delim and Tmpl_r_delim, for example, we define in the project configuration file:

' Tmpl_l_delim ' = ' <{',  ' tmpl_r_delim ' = '}> ',

Then, the above variable output label should be changed to:

<{$name}>

We all use the default label definition to illustrate that the first parameter in the Assign method is the name of the variable used in the template file, if it is changed to the following code:

$name = ' thinkphp '; $this->assign (' name2 ', $name);

The {$name} output is invalid and you must use {$name 2} to output the value of the template variable. If we need to assign a user data object to a template variable:

$User = M (' name '); $user = $User->find (1); $this->assign (' User ', $user);

That is, $user is actually an array variable, and we can use the following method to output the associated value:

{$user [' name ']}//output user's names {$user [' email ']}//output user's email address

If $user is an object and not an array.

$User = M (' name '); $User->find (1); $this->assign (' User ', $User);

The related property values can be output in the following ways:

{$user: name}//output User's name {$user: email}//output user's email address

After version 3.1, the class's attribute output is adjusted to support native PHP object notation, so the above label needs to be changed to:

{$user->name}//output user's name {$user->email}//output user's email address

To facilitate template definition, you can also support point syntax, for example, the above

{$user [' name ']}//output user's names {$user [' email ']}//output user's email address

can be changed into

{$user. Name} {$user. Email}

Because the default output of point syntax is array mode, the above two methods are equivalent without configuration, we can determine the output effect of point syntax by configuring the Tmpl_var_identify parameter, take the following output as an example: {$user. Name}

If Tmpl_var_identify is set to array, then

{$user. Name} and {$user [' name ']} are equivalent, which is the output array variable.

If Tmpl_var_identify is set to obj, then

{$user. Name} and {$user: name} are equivalent, that is, the properties of the output object.

If Tmpl_var_identify is left blank, the system automatically determines whether the variable to be output is an array or an object, which in some way affects efficiency, and supports only two-dimensional arrays and two-level object properties.

If the output of a multidimensional array or multi-layered object property, you can use the following definition:

{$user. sub.name}//using Point syntax output

or use

{$user [' Sub '] [' name ']}//output the value of the three-dimensional array  {$user: sub:name}//the Multilevel property of the output object

The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!

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.