Variable output of THINKPHP template engine

Source: Internet
Author: User
We already know that The assign method can be used in Action to assign values to template variables. how can we output the values of variables in the template file after assigning values? If we assign a name mod in the Action...

We already know that The assign method can be used in Action to assign values to template variables. how can we output the values of variables in the template file after assigning values?

If we assign a name template variable to the Action:

  1. $ Name = 'thinkphp ';
  2. $ This-> assign ('name', $ name );

To use the built-in template engine to output variables, you only need to use the following in the template file:

{$ Name}

The template compilation result is

The output result of ThinkPHP can be displayed at the tag position during the final running. Note that no space is allowed between {and $ of the template tag; otherwise, the tag is invalid. By default, the start tag is {and the end tag is}. you can also set TMPL_L_DELIM and TMPL_R_DELIM to make changes. for example, we define in the project configuration file:

  1. 'Tmpl _ L_DELIM '=>' <{',
  2. 'Tmpl _ R_DELIM '=>'}> ',

Then, the variable output label above should be changed:

<{$ Name}>

The following content is described in the default tag definition. The first parameter in the assign method is the variable name used in the template file. if it is changed to the following code:

  1. $ Name = 'thinkphp ';
  2. $ This-> assign ('name2', $ name );

The output using {$ name} is invalid. you must use {$ name2} to output the template variable value. if you need to assign a user data object to the template variable:

  1. $ User = M ('name ');
  2. $ User = $ User-> find (1 );
  3. $ This-> assign ('user', $ user );

That is to say, $ user is actually an array variable. we can use the following method to output related values:

  1. {$ User ['name']} // output the user name
  2. {$ User ['email ']} // output the user's email address

If $ user is an object rather than an array.

  1. $ User = M ('name ');
  2. $ User-> find (1 );
  3. $ This-> assign ('user', $ user );

You can use the following method to output relevant property values:

  1. {$ User: name} // name of the output user
  2. {$ User: email} // output the user's email address

Later than version 3.1, the output method of class attributes has been adjusted, and native PHP object writing is supported. Therefore, the above label needs to be changed:

  1. {$ User-> name} // name of the output user
  2. {$ User-> email} // output the user's email address
  3. To facilitate template definition, you can also support the dot syntax, for example, the preceding
  4. {$ User ['name']} // output the user name
  5. {$ User ['email ']} // output the user's email address

Can be changed

  1. {$ User. name}
  2. {$ User. email}

Because the default output of the dot syntax is the array method, the above two methods are equivalent without configuration. we can configure the TMPL_VAR_IDENTIFY parameter to determine the output effect of the dot syntax, the following output is used as an example: {$ user. name}

If TMPL_VAR_IDENTIFY is set to array

{$ User. name} is equivalent to {$ user ['name']}, that is, the output array variable.

If TMPL_VAR_IDENTIFY is set to obj

{$ User. name} is equivalent to {$ user: name}, that is, the attribute of the output object.

If TMPL_VAR_IDENTIFY is left empty, the system automatically determines whether the output variable is an array or an object. this method affects efficiency to a certain extent and only supports two-dimensional arrays and two-level object attributes.

For multi-dimensional array or multi-layer object attribute output, you can use the following definition method:

{$ User. sub. name} // use the dot syntax to output

Or use

  1. {$ User ['sub'] ['name']} // outputs the value of the 3D array.
  2. {$ User: sub: name} // multi-level attribute of the output object
Related Article

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.