ThinkPHP3.1 quick start (9) variable output

Source: Internet
Author: User
In the previous chapter, we learned how to assign variables to template variables through the assign method. This article details how to use tags to output Template variables in templates. In the previous chapter, we learned how to assign variables to template variables through the assign method. This article details how to use tags to output Template variables in templates.
Note: the description in this article is only applicable to the use of the internal template engine. if you use Smarty or other template engines, refer to the relevant variable output syntax.

The method for outputting variables is very simple. for example, in the controller, we assign values to the template variables:
  1. $ Name = 'thinkphp ';
  2. $ This-> assign ('name', $ name );
  3. $ This-> display ();
Copy the code and use it in the template:
  1. Hello, {$ name }!
The compiled result of copying the code template is:
  1. Hello, !
Copy the code so that it will be displayed in the template during running:
  1. Hello, ThinkPHP!
Copy the code [-more-]
Note that no space is allowed between {and $ of the template tag; otherwise, the tag is invalid. Therefore, the following labels
  1. Hello, {$ name }!
Copying the code does not output the name variable normally, but directly keeps the output unchanged:
  1. Hello, {$ name }!
By default, the start tag is {and the end tag is }. You can also change the value by setting TMPL_L_DELIM and TMPL_R_DELIM. For example, we define in the project configuration file:
  1. 'Tmpl _ L_DELIM '=>' <{',
  2. 'Tmpl _ R_DELIM '=>'}> ',
Copy the code so that the above variable output tag should be changed:
  1. Hello, <{$ name}>!
The content after the copy code is described in the default tag definition.
The variable output of the template tag varies according to the variable type. we just output the string variable. if it is an array variable,
  1. $ Data ['name'] = 'thinkphp ';
  2. $ Data ['email '] = 'thinkphp @ qq.com ';
  3. $ This-> assign ('data', $ data );
Copy the code so that we can output it in the template in the following way:
  1. Name: {$ data. name}
  2. Email: {$ data. email}
Copying code or using the following method is also valid:
  1. Name: {$ data ['name']}
  2. Email: {$ data ['email ']}
Copy code when we want to output multi-dimensional arrays, we usually need to use the following method.
If the data variable is an object (which includes two attributes: name and email), you can output the data in the following way:
  1. Name: {$ data: name}
  2. Email: {$ data: email}
Copy the code or
  1. Name: {$ data-> name}
  2. Email: {$ data-> email}
Copy code
System variables common Template variables must be assigned values before they can be output in the template. However, system variables are not required and can be output directly in the template, the output of system variables is usually starting with {$ Think, for example:
  1. {$ Think. server. script_name} // output the $ _ SERVER ['script _ name'] variable.
  2. {$ Think. session. user_id} // output the $ _ SESSION ['User _ id'] variable.
  3. {$ Think. get. pageNumber} // output the $ _ GET ['pagenumber'] variable.
  4. {$ Think. cookie. name} // Output $ _ COOKIE ['name'] variable
The copied code can output $ _ SERVER, $ _ ENV, $ _ POST, $ _ GET, $ _ REQUEST, $ _ SESSION, and $ _ COOKIE variables.
You can also output constants.
  1. {$ Think. const. MODULE_NAME}
Copy code or directly use
  1. {$ Think. MODULE_NAME}
Copy the code output configuration parameters and use:
  1. {$ Think. config. db_charset}
  2. {$ Think. config. url_model}
To copy the code output language variables, you can use:
  1. {$ Think. lang. page_error}
  2. {$ Think. lang. var_error}
Copy code
When using functions, we often need to use functions for the template output variables. you can use:
  1. {$ Data. name | md5}
After the code is compiled, the result is:
Copy the code. if the function has multiple parameters that need to be called, use:
  1. {$ Create_time | date = "y-m-d ",###}
Copy the code to input two parameters in the date function. each parameter is separated by a comma. here, the first parameter is y-m-d, and the second parameter is the create_time variable to be output, because this variable is the second parameter, you must use ### to identify the variable location. the compiled result is:
Copy the code. if the output variable is the first parameter of the function defined later, you can directly use:
  1. {$ Data. name | substr = 0, 3}
Copy code to indicate output
You can also copy the code:
  1. {$ Data. name | substr}
Copy the code, but it is useless.

You can also filter multiple functions. Separate multiple functions with "|". for example:
  1. {$ Name | md5 | strtoupper | substr = 0,3}
After the code is compiled, the result is:
The copy code function is called sequentially from left to right.
If you think it is troublesome to write this code, you can directly write it like this:
  1. {: Substr (strtoupper (md5 ($ name), 0, 3 )}
Copy code
By default, we can provide the default value for the output of the variable, for example:
  1. {$ User. nickname | default = "this guy is very lazy and has nothing left "}
The copy code still supports default output for system variables, for example:
  1. {$ Think. get. name | default = "name is blank "}
Copy the default value of the code and the function can be used at the same time, for example:
  1. {$ Think. get. name | getName | default = "name blank "}
Copy code
We can use operators for template output, including support for "+" "-" "*" "/" and "%.
For example:
Operator Example
+ {$ A + $ B}
- {$ A-$ B}
* {$ A * $ B}
/ {$ A/$ B}
% {$ A % $ B}
++ {$ A ++} or {++ $}
-- {$ A --} or {-- $}
Integrated operation {$ A + $ B * 10 + $ c}
When using operators, point syntax and common function usage are no longer supported. for example:
  1. {$ User. score + 10} // incorrect
  2. {$ User ['score '] + 10} // correct
  3. {$ User ['score '] * $ user ['level']} // correct
  4. {$ User ['score '] | myFun * 10} // incorrect
  5. {$ User ['score '] + myFun ($ user ['level'])} // correct
Copy code
In summary, we learned how to output variables and use functions, default values, and operators in the template file, in the next article, we will learn how to control the output of template variables such as loop and judgment, and import other public templates.

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.