Well. thinkphp03

Source: Internet
Author: User
Tags echo date

Template

Data, page separation... data is transmitted to the page, and the page displays data only in the corresponding position ....

Template assignment and Output

$ This-> assign ('name', $ value); or

$ This-> name = $ value;

Output 

1. Default output$ This-> display ();

2. Output other operation templates of the current Module
 $ This-> display ('user ');
3. Output operation templates for other modules   $ This-> display ('[Group Name:] Module name: Operation name'); group name is optional. If project group is enabled, if this parameter is not set, the default group is used.

4. Output operation templates for other themes 
$ This-> display ('topic name @ Module name: Operation name ');

5. Full path output Template         
$ This-> display ('./TPL/default/index/index.html ');

6. Specify the page Encoding           
$ This-> display ('template', 'code ');
7. other ....

 

Use {$ variable name} to retrieve data on the page

If the output template variable has no value, but we need to assign a default value when displaying it, we can use default
Syntax to display a value by default. {$ Username | default = "anonymous "}
Use the {$ variable | function 1 |... | function n = parameter 1,..., parameter n, ###} for template Variables ,###}
{$ Webtitle | MD5 | strtoupper | substr =}. use MD5 once from left to right, convert it to uppercase, and take the substring ..

If the execution result of a variable or function is not the first parameter of the function or the next function, you must use the "###" positioning character:

{$userinfo["regdate"]|date="Y-m-d H:i",###}

Equivalent   Echo date ("Y-m-d
H: I ", $ userinfo [" regdate "] | );

The template file also supports convenient methods for directly calling functions, without using template variables. There are two methods:

{: Say_hello ('thinkphp')} // say_hello () is a user-defined function.
, Display the return value
{~ Say_hello ('thinkphp')} // say_hello () is a user-defined function. If no return value is displayed, only execution is performed.

Output System VariablesIncluding server, session, post, get, request, cookie, and Env. The output of system variables does not need to be assigned to a template variable in advance. The output of system variables is as follows:
$ Think. Starts with and supports functions.

{$ Think. session. session_id | MD5} // output the $ _ session variable and use MD5
Encryption
// Or abbreviated
{$ _ Session. session_id | MD5}
// Output $ _ Get variable
{$ _ Get. pagenumber}

Use $ think. const to output system constants.

{$ Think. Const. action_name} // output the thinkphp system-defined constant
Action_name (current operation name)
// Or abbreviated
{$ Think. action_name}

Use $ think. configOutput the configuration parameter value of the project.

{$ Think. config. db_prefix}

The output value is the same as the returned result of C ('db _ prefix.

Quick output (not recommended ////)

To make the template definition more concise, the system also supports some common variables to output quick labels, including:

  • {@ Var} is equivalent to {$ think. session. var} and outputs the session variable.
  • {# Var} is equivalent to {$ think. Cookie. var}, and the cookie variable is output.
  • {& Var} is equivalent to {$ think. config. var} and outputs the configuration parameters.
  • {% Var} is equivalent to {$ think. Lang. var}, and the output language variable
  • {. Var} is equivalent to {$ think. Get. var}, and the get variable is output.
  • {^ Var} is equivalent to {$ think. Post. var} and outputs the post variable.
  • {* Var} is equivalent to {$ think. Const. var}, and the output constant

Module inclusion

1. Other template operation files of the current module <include file = "Operation name"
/>

2. Operation modules of other modules      
<Include file = "module name: Operation name"/>
3. Complete file name inclusion           <Include
File = "complete template file name"/> contains the extension
4. The module operation template containing other themes <include file = "topic name @ Module name: Operation name"
/>

5. Use variables to control the import Template    
<Include file = "$ variable name"/>
6. shortcuts                
{Include: Template File rules }......

Module Import and load keywords

Import Javascript
Example:      <Import
Type = 'js' file = "Js. Common"/>

Multiple files can be imported in batches, separated by commas (,): <import type = 'js'
File = "Js. Common, JS. util. Date"/>

Import CSS
Example:           
 <Import type = 'css'
File = "CSS. Style"/>

Set the basepath attribute to the public directory. <import file = "JS"
File = "Js. Common" basepath = "/common"/>

The load command is consistent with the import command, used to import external JS or CSS files.

Example: <load href = "/public/JS/common. js"
/> <Load href = "/public/CSS/style.css"
/>

In practice, considering the readability of the template, we recommend that you use the "traditional import + special template tag replacement" or "LOAD + special template tag replacement" method.
Bytes
O (Clerk □clerk) O

Special string replacement

The thinkphp system defines some special strings (constants) for ease of use in the template. These special strings bring great convenience to the template application.

These special strings are automatically replaced before template output. The default template special strings are as follows:

  • ../Public: Public template directory of the current project, usually/project directory/TPL/default/public/
  • _ Public __: Public directory of the current website, usually/public/
  • _ Tmpl __: The template directory of the project, usually/project directory/TPL/default/
  • _ Root __: The address of the current website (excluding the domain name)
  • _ App __: The URL of the current project (excluding domain names)
  • _ URL __: URL of the current module (excluding domain names)
  • _ Action __: The URL of the current operation (excluding domain names)
  • _ Self __: Current page url

Thinkphp allows you to change the default replacement rules or even define your own template to replace special strings. Configure in the project configuration file
The tmpl_parse_string option is used to change or add a template to replace the string, for example:

Tmpl_parse_string => array ('_ Public _' => '/common ', // change the default _ Public _ replacement rule '_ upload _' => '/public/uploads/', // Add a new upload path replacement rule)

 

Tag

Volist loop output tag

1. If you want to output part of the data in the result set, you must specify the offset (Data Pointer) and length (number of data records) attributes.

<Volist name = "list" id = "VO" offset = "5"
Length = '10'>

2. The MOD parameter in volist is equivalent to specifying a frequency, and the system will
Parameter Value remainder (% operator in PHP) operation. Combined with tags (such as EQ tags), you can control the output data or data display format based on the frequency.

<Table>
<Volist name = "list" id = "VO"
MoD = "2">
<Tr <EQ name = "Mod"
Value = "0">
Style = "background-color: # FFF;" </EQ>

   
<TD> I am a cell content </TD>

   
<TD> I am also a cell content </TD>

</Tr>
</Volist>
</Table>

3. specify the number of variables used by the key attribute to output the loop (not the primary key ID of the data table): 1, 2, 3, 4, 5, 6, 7

4. Use the $ key variable directly to output the array index:

<Volist name = "list" id = "VO">
Array key: {$ key} <br/>
User name: {$ VO ['username']} <br/>
Email: {$ VO ['email ']} <br/>
Registration Time: {$ VO ['regdate'] | date = "Y-m-d h: I ",###}
<HR/>
</Volist>

Different from the output loop variable, the key value depends on the data, rather than the volist loop output.

Volist nested output multi-dimensional array

Foreach label

Without the many functions of the volist tag, the advantage is that the object can be traversed and output, while the volist tag is usually used to output arrays.
<Foreach name = "list" id = "VO">
User name: {$ VO. Username} <br/>
Email: {$ VO. Email} <br/>
Registration Time: {$ VO. regdate | date = "Y-m-d h: I ",###}
<HR/>
</Foreach>

Switch label

The thinkphp template engine supports switch judgment and outputs different values based on different situations. The format is as follows:

<Switch name = "variable name">
<Case
Value = "value 1"> output content 1 </case>

<Case
Value = "value 2"> output content 2 </case>

<Default
/> Default
</Switch>

The Value Attribute of case can support simultaneous judgment of multiple conditions. Use the | symbol for segmentation:

<Witch name = "_ Get. Type">
<Case
Value = "GIF | PNG | jpeg"> Image Format File </case>

<Default/> other format files
</Switch>
Judge the output comparison tag

<Compare tag name = "variable name"
Value = "value"> output content </compare tag>

 <EQ name = "username"
Value = "admin"> administrator </EQ>

Like other labels, the property value of name is the variable name and does not require the $ symbol. When the Value Attribute uses the variable, the $ symbol is required:

<EQ name = "uid"
Value = "$ adminid"> administrator </EQ>

Comparison labels can be used in combination with else labels: <EQ name = "username"
Value = "admin"> administrator <else/> masses </EQ>
Determines whether a variable is assigned a value.

The present tag is used to determine whether the template variable has been assigned a value, which is equivalent to the isset () function behavior in PHP.

There is also a notpresent tag, which is the opposite of the present tag (that is! Isset ()):

<Notpresent
Name = "username"> Username
Variable has not been assigned value </notpresent>

<Present name = "username"> Username
The variable has been assigned <else/> Username
Variable has not been assigned value </present>
The defined tag is used to determine whether a constant has been defined, which is equivalent to the defined () function behavior in PHP. There is another
Notdefined tag

The empty tag is used to determine whether the template variable is null, which is equivalent to the empty () function action in PHP.

<Empty
Name = "variable name"> content to be output </empty>

There is also a notempty tag,

Range judgment

The in tag is used to determine whether a template variable is within a certain range. The format is as follows:

<In name = "variable name"
Value = "value 1, value 2,..."> content to be output </In>

There is also a notin tag, which is an in tag.

In and notin labels can also be replaced by range labels:

<Range name = "groupid" value = "1, 2, 3" type = "in"
> Manage groups </range>

The preceding example is equivalent to the In tag. When the value of the type attribute is notin, it is equivalent to the notin tag.

Judge the output if... else
<If
Condition = "expr1"> output content 1
<Elseif condition = "expr2"
/> Output content 2
<Else/> output content 3
</If>

<If condition = "($ VO ['uid'] EQ 1) or ($ VO ['username']
EQ 'admin') "> Administrator
<Elseif condition = "$ VO ['uid'] GT 1"
/> Masses
<Else/> tourists
</If>

Like expressions in database operations, expressions in condition judgment cannot use common expressions such
=,>, <, And other judgment symbols in PHP comparison operators. thinkphp-specific comparison labels are required:

Conditional conditions support array and object judgment and automatic judgment of arrays or objects:

// Array format
<If condition = "$ VO ['uid'] EQ
1 ">
// Object format
<If condition = "$ VO: uid EQ 1">
// Automatic judgment
<If condition = "$ VO. uid EQ 1">

PHP code can also be directly used in the condition attribute, for example:

<If condition = "strtoupper ($ VO ['username']) eq
'Admin' "> Administrator
<Else/> masses
</If>
TIPS:

  • Use more concise comparison labels and
    Switch
    Tags.
  • If the judgment labels provided by thinkphp still cannot meet special requirements, you can directly use native PHP code or PHP
    Tags.

 

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.