thinkphp template engine variable output detailed _php example

Source: Internet
Author: User
Tags closing tag

This article analyzes the usage of variable output in the 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, how to output the value of a variable in a template file after assigning a value?

If we assign a name template variable to the action:

Copy Code code as follows:
$name = ' thinkphp ';
$this->assign (' name ', $name);

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

After the template is compiled, the result is

Copy Code code as follows:
<?php Echo ($name);? >

At the end of the run can be displayed in the label location thinkphp output, note that the template label {and $ can not have any space between, otherwise the label is invalid. The normal label default start tag is {, the closing 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:

Copy Code code as follows:
' Tmpl_l_delim ' => ' <{',
' Tmpl_r_delim ' => '}> ',

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

<{$name}>

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

Copy Code code as follows:
$name = ' thinkphp ';
$this->assign (' name2 ', $name);

The use of {$name} output is invalid and {$name 2} must be used to output the value of the template variable. If we need to assign a user data object to a template variable:
Copy Code code as follows:
$User = M (' name ');
$user = $User->find (1);
$this->assign (' user ', $user);

That is, $user is actually an array variable, we can use the following way to output the relevant values:
Copy Code code as follows:
{$user [' name ']}//the name of the output user]
{$user [' email ']}//output user's email address

If $user is an object rather than an array.
Copy Code code as follows:
$User = M (' name ');
$User->find (1);
$this->assign (' user ', $User);

You can output related property values in the following ways:
Copy Code code as follows:
{$user: name}//the name of the output user
{$user: Emails}//output user's email address

After the 3.1 version, the class's property output has been adjusted to support native PHP object writing, so the above label needs to be changed to:
Copy Code code as follows:
{$user->name}//the name of the output user
{$user->email}//output user's email address

To facilitate template definitions, you can also support point syntax, such as the
Copy Code code as follows:
{$user [' name ']}//the name of the output user]
{$user [' email ']}//output user's email address

can be changed into
Copy Code code as follows:
{$user. Name}
{$user. Email}

Since the default output of the point syntax is an array, the above two methods are equivalent in the absence of configuration, and we can configure the Tmpl_var_identify parameter to determine the output effect of the point syntax, with the following output as an example: {$user. Name}

If Tmpl_var_identify is set to array, then

{$user. Name} is equivalent to {$user [' name ']}, 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 the tmpl_var_identify is left blank, the system automatically determines whether the variable to be output is an array or an object, which affects efficiency to some extent, and supports only two-dimensional arrays and level two object properties.

If the output is a multidimensional array or a Multilayer object property, you can use the following definition:

Copy Code code as follows:
{$user. sub.name}//using dot syntax output

or use

Copy Code code as follows:
{$user [' Sub '] [' name ']]}//output three-dimensional array value
{$user: sub:name}//output object's multilevel properties

I hope this article will help you with the PHP program design based on thinkphp framework.

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.