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 of great practical value, you can refer to the examples in this article to analyze the usage of variable output in the ThinkPHP template engine. Share it with you for your reference. The specific analysis is as follows:
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:
The code is as follows:
$ Name = 'thinkphp ';
$ 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 code is as follows:
<? Php echo ($ name);?>
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:
The code is as follows:
'Tmpl _ L_DELIM '=>' <{',
'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:
The code is as follows:
$ Name = 'thinkphp ';
$ 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:
The code is as follows:
$ User = M ('name ');
$ User = $ User-> find (1 );
$ This-> assign ('user', $ user );
That is to say, $ user is actually an array variable. we can use the following method to output related values:
The code is as follows:
{$ User ['name']} // output the user name
{$ User ['email ']} // output the user's email address
If $ user is an object rather than an array.
The code is as follows:
$ User = M ('name ');
$ User-> find (1 );
$ This-> assign ('user', $ user );
You can use the following method to output relevant property values:
The code is as follows:
{$ User: name} // name of the output user
{$ 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:
The code is as follows:
{$ User-> name} // name of the output user
{$ User-> email} // output the user's email address
To facilitate template definition, you can also support the dot syntax, for example, the preceding
The code is as follows:
{$ User ['name']} // output the user name
{$ User ['email ']} // output the user's email address
Can be changed
The code is as follows:
{$ User. name}
{$ 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:
The code is as follows:
{$ User. sub. name} // use the dot syntax to output
Or use
The code is as follows:
{$ User ['sub'] ['name']} // outputs the value of the 3D array.
{$ User: sub: name} // multi-level attribute of the output object
I hope this article will help you design PHP programs based on the ThinkPHP framework.