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:
- $ 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 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:
- '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:
- $ 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:
- $ 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:
- {$ User ['name']} // output the user name
- {$ User ['email ']} // output the user's email address
If $ user is an object rather than an array.
- $ User = M ('name ');
- $ User-> find (1 );
- $ This-> assign ('user', $ user );
You can use the following method to output relevant property values:
- {$ 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:
- {$ 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
- {$ User ['name']} // output the user name
- {$ User ['email ']} // output the user's email address
Can be changed
- {$ 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:
{$ User. sub. name} // use the dot syntax to output
Or use
- {$ User ['sub'] ['name']} // outputs the value of the 3D array.
- {$ User: sub: name} // multi-level attribute of the output object