Home > Backend Development > PHP Tutorial > What are the methods for calling ThinkPHP template variable output?

What are the methods for calling ThinkPHP template variable output?

WBOY
Release: 2016-07-25 08:53:08
Original
1450 people have browsed it
  1. {$Variable name} Example: {$username} //Ordinary variable {$userinfo["email"]} //Array mode {$userinfo["sub"]["name"]} //Three-dimensional Array mode {$userinfo:email} //Object mode {$userinfo.email}...
Copy code

Normal output Output the variables in the template in the following format: {$Variable name}

Example:

  1. {$username} //Ordinary variables
  2. {$userinfo["email"]} //Array method
  3. {$userinfo["sub"]["name"]}//Three-dimensional array method
  4. { $userinfo:email} //Object mode
  5. {$userinfo.email} //Automatically determine array or object mode
Copy code

Default output If the output template variable has no value, but we need to assign a default value when displaying, we can use the default syntax to display a value by default.

Format: {$variable|default="default value"} example:

  1. {$username|default="Anonymous"}
Copy code

Use function Using functions with template variables The template engine supports formatting of output variables, that is, using functions, and supports multiple functions.

Format: {$variable|function1|...|functionn=parameter1,...,parametern,###} The function executes the variables from left to right. Function 1 is executed first. After the result is obtained, it is used as a parameter and then function 2 is executed, and so on. By default, the execution result is used as the first parameter of the next function.

Example:

  1. {$webTitle|md5|strtoupper|substr=0,3}
Copy the code

The actual execution result is equivalent to: If the variable or the result of the previous function execution is not the first parameter of the function or the next function, then the locator "###" needs to be used: {$userinfo["regdate"]|date="Y-m-d H:i",###} The actual execution result is equivalent to:

  1. echo date("y-m-d H:i",$userinfo["regdate"]|);
  2. ?>
Copy code

Tips There is no limit on the number of functions you can use with variables, but the exit and echo functions are disabled by default to prevent corrupting template output. For specific configuration of disabled functions, please refer to "ThinkPHP System Configuration".

Template uses functions directly Template files also support shortcut methods for calling functions directly without passing template variables, including two methods:

1. Execute the function and output the return value

Format: {:function} example:

  1. {:say_hello('ThinkPHP')} //say_hello() is a user-defined function
  2. The actual execution result is equivalent to:
  3. echo say_hello('ThinkPHP');
  4. ?> ;
Copy code

2. Execute the function but not output Format: {~function} example:

  1. {~say_hello('ThinkPHP')} //say_hello() is a user-defined function
  2. The actual execution result is equivalent to:
  3. say_hello('ThinkPHP');
  4. ?>
Copy code

The above two methods also support passing in template variables as parameters of the function.

System variable output System variables include: server, session, post, get, request, cookie, env. The output of system variables does not need to be assigned to a template variable in advance. The output of system variables begins with $Think., and the use of functions is still supported.

Example:

  1. {$Think.session.session_id|md5} //Output the $_SESSION variable and use md5 encryption
  2. //Or abbreviated as
  3. {$_SESSION. session_id|md5}
  4. //Output the $_GET variable
  5. { $_GET.pageNumber}
Copy code

System constant output Use $Think.const to output system constants.

Example:

  1. {$Think.const.ACTION_NAME} //Output the ThinkPHP system-defined constant ACTION_NAME (current operation name)
  2. //or abbreviated as
  3. {$Think.ACTION_NAME}
Copy code

Configuration parameter output Use $Think.config to output the project's configuration parameter values.

Example:

  1. {$Think.config.DB_PREFIX}
Copy code

The output value is the same as the return result of C('DB_PREFIX').

Quick output In order to make the template definition more concise, the system also supports some commonly used variable output shortcut tags, including:

  1. {@var} is equivalent to {$Think.session.var}, the output Session variable
  2. {#var} is equivalent to {$Think.cookie.var}, the output Cookie variable
  3. {&var} is equivalent to {$Think.config.var} is equivalent, the output configuration parameter
  4. {%var} is equivalent to {$Think.lang.var}, and the output language variable
  5. {.var} is equivalent to {$Think.get.var} , the output GET variable
  6. {^var} is equivalent to {$Think.post.var}, the output POST variable
  7. {*var} is equivalent to {$Think.const.var}, the output constant
Copy code

Tips Quick output does not support the use of functions In order to make the template more readable, it is not recommended to use the shortcut output method



source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template