View 2 in ThinkPHP-nannong trail

Source: Internet
Author: User
View in ThinkPHP's video-nannong gossip ThinkPHP 1. template comment

In actual project development, the annotation function is often used. if it is a ThinkPHP framework, you can use the following method in the template file to annotate:

{// Comment content}: Single line comment

{/* Comment content */}: multi-line comment

 

Sample code:

Run the above code to know that the template comment in the ThinkPHP framework belongs to the server comment and is not displayed in the client browser.

2. fetch get template content

In actual project development, if we only want to obtain the template content but do not output it, we can use the fetch method for implementation.

$ This-> fetch (): ① load template ② replace variable

$ This-> display (): ① load template ② replace variable ③ output template content

 

Sample code:

3. variable output

In the ThinkPHP template engine, you can use the assign method to allocate variables to the template file and output them. the assigned variables can be common variables, array variables, and object variables.

 

1) common variables

In the template, you can use {$ variable name} to access it:

 

2) array variables

① One-dimensional array

You can use the following method to display the output in the template:

② Two-dimensional array

You can access the template file as follows:

 

3) object variables

You can access the template file as follows:

4. system variables
  • $ Think. server: $ _ SERVER []

  • $ Think. get: $ _ GET []

  • $ Think. post: $ _ POST []

  • $ Think. request: $ _ REQUEST []

  • $ Think. cookie: $ _ COOKIE []

  • $ Think. session: $ _ SESSION []

  • $ Think. config: Read the configuration information in the configuration file.

 

Sample code:

Running result:

5. use a function (variable regulator)

Main functions: formatting Variables. basic syntax:

{$ Name | fn1 | fn2 = arg1, arg2 ,###}

Note: When using a function, it has a special form: ### representing the current variable itself

 

Sample code:

6. default value

In actual project development, a variable is often judged. if it is empty, it will not display any content, but the user experience is not very friendly, so to solve this problem, consider using the default value for settings:

{$ Variable | default = "default value "}

7. Operators

Some Template engines, such as Smarty, cannot directly participate in mathematical operations. However, in the ThinkPHP template engine, it allows direct mathematical operations. the basic syntax is as follows:

  • + {$ A + $ B}

  • -{$ AB}

  • * {$ A * $ B}

  • /{$ A/$ B}

  • % {$ A % $ B}

  • ++ {$ A ++} or {++ $}

  • -- {$ A --} or {-- $}

Sample code:

On the template page, you can use operators to perform mathematical operations on the preceding two variables:

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.