Assign we can assign a template variable to an Action using the assign method. How do we output the value of the template variable to the template file?

If we assign a name template variable to the Action:

  • $name = ‘ThinkPHP’;
  • $this->assign(‘name’,$name);

To use the built-in template engine output variable, just use it in the template file:

{$name}

The result of compiling the template is


When you finally run it, you can display the ThinkPHP output in the tag position. Note that the template tag must not have any Spaces between {and $, otherwise the tag will not be valid. The default start tag for normal tags is {and the end tag is}, which can also be changed 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 variable output tag above should be changed to:

<{$name}>

The first parameter in the assign method is the name of the variable to be used in the template file.

  • $name = ‘ThinkPHP’;
  • $this->assign(‘name2’,$name);

{$name} will not work. You must use {$name2} to print 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);

$user is an array variable, and we can print the associated values as follows:

  • {$user[‘name’]}// Outputs the user name
  • {$user[’email’]} // Displays the email address of the user
If $user is an object rather than an array.

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

You can print the associated attribute values as follows:

  • {$user:name}// Outputs the user name
  • {$user:email} // Displays the email address of the user
After version 3.1, the output method of class attributes has been changed to support the native PHP object writing method, so the above tag needs to be changed to:

  • {$user->name}// Outputs the user name
  • {$user->email} // Displays the email address of the user
  • To facilitate template definition, you can also support dot syntax, for example, above
  • {$user[‘name’]}// Outputs the user name
  • {$user[’email’]} // Displays the email address of the user
You can change to

  • {$user.name}
  • {$user.email}

{$user.name} If the TMPL_VAR_IDENTIFY parameter is set to TMPL_VAR_IDENTIFY, the output of the TMPL_VAR_IDENTIFY parameter is set to {$user.name}.

If TMPL_VAR_IDENTIFY is set to array, then

{$user. Name} and {$user[‘name’]} are equivalent, i.e. output array variables.

If TMPL_VAR_IDENTIFY is set to obj, then

{$user. Name} is equivalent to {$user:name}, which is the property 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 affects efficiency to some extent, and only two-dimensional arrays and two-level object attributes are supported.

If the output is a multi-dimensional array or multi-layer object attribute, you can use the following definition:

{$user.sub.name}// Use dot syntax output

Or use

  • {$user[‘sub’][‘name’]}// Outputs the value of the 3d array
  • {$user:sub:name}// Output multilevel attributes of the object