thinkphp--Template Basics

Source: Internet
Author: User

A Variable output
It is very easy to output variables in the template, using the Assign () method, passing variables and values in key-value pairs.
Pass a variable to the template
$user = ' new ';
$this->assign (' user ', $user);
You can call this in the template:
{$user}
The results of the template compilation are:
<?phpecho ($user);? >//Can be viewed at runtime
The template label {and $ cannot be separated by any spaces, otherwise it cannot be resolved. If you want to change two {} can be set:
Modify Default Label
' Tmpl_l_delim ' = ' <{',
' Tmpl_r_delim ' = '}> ',
The invocation method must be changed:
<{$user}>
If you pass an array, we pass the past directly, and call it in two ways:
Passing an array to a template
$data [' User ']= ' new ';
$data [' Email ']= ' [email protected] ';
$this->assign (' data ', $data);
The method is called:
user:{$data. user}email:{$data. Email}
user:{$data [' User ']}email:{$data [' Email ']}
If we pass an object, we pass it directly past, and call it in two ways:
Passing an object to the template
$data =new\stdclass ();
$data->user= ' new ';
$data->email= ' [email protected] ';
$this->assign (' data ', $data);
The method is called:
user:{$data->user}email:{$data->email}
user:{$data: user}email:{$data: Email}
Two System variables
In the template, you can not only output PHP system variables, but also output thinkphp system variables.
Output PHP System variables
{$Think. Server.script_name}//$_server[' Script_name ')
{$Think. session.admin}//$_session[' admin ']
{$Think. Get.user}//$_get[' user ']
{$Think. Post.user}//$_post[' user ']
{$Think. Request.user}//$_request[' user ']
{$Think. cookie.name}//$_cookie[' name ']
System Variables for thinkphp
{$Think. Const.app_path}//Directory
{$Think. Config.url_model}//url Mode
{$Think. Lang.var_error}//Language variable
Three Using functions
If sometimes we need to use PHP functions in the template, we can use the following format:
{$user |MD5}
If you have more than one parameter to pass, you can refer to the following settings:
{$date |date= "y-m-dh:i:s", # # #}
PS: Indicates that the date function passed in two parameters, each parameter is separated by a comma, here the first parameter is y-m-d
H:i:s, the second argument is the date variable that you want to output earlier, because the variable is the second argument, so you need to use # # #
Identifies the variable location, and the compiled result is:
<?phpecho (Date ($date, "y-m-dh:i:s")); >
The preceding output variable, defined later, does not require # # #
{$user |mb_substr=0,3, ' UTF-8 '}
Multiple functions with "|" Separated can
{$user |mb_substr=0,3, ' UTF-8 ' |MD5|SHA1}
PS: If you think the above writing needs to be translated in mind two times, too much trouble, then you can use the following lattice
Type of wording:
{: MD5 (MB_SUBSTR ($user, 0,3, ' UTF-8 ')}
PS: I personally think that the data processing, as far as possible, not the template end, unless it is necessary to be at the template end
Management. Because the template is for HTML and JS, and then is to provide data, do not include a variety of filtering and business logic.
The template provides the default output function when the passed variable has no value.
{$user |default= ' nothing! ‘}
Four Using operators
We can use the operators in the template, including the "+", "-", "*", "/", "%", "--" and "+ +"
Support.
{$data [' num ']+10}//Bracket
{$data [' num ']+getnum ()}//function
PS: Syntax and general function usage are no longer supported when using operators:
{$data. num+10}//Wrong
{$data [' num ']+getnum}//Error
The template also supports ternary operators:
{$user? ' Value ': ' No value '}
Five Include file
In a system, you can include generic header files and foot files: headers and footer. Because each page
The head feet are the same, so they need to be separated separately, and then introduced into them with the inclusion file.
We can create a public directory under the default theme directory in the View directory, which is specifically stored
Put the public call template file.
<includefile= ' Public/header '/>
<includefile= ' Public/footer '/>
You can also call the absolute path pattern:
<includefile= './weibo/home/view/default/public/header.tpl '/>
Multiple template files are also called:
<includefile= ' Public/header,public/footer '/>
Six Template notes
Template support annotation function, provide a template for the production staff reference.
{//This is a comment}
{/* This is also a comment */}
{/* This is
Multi-line Comment */}
Seven Template inheritance
Template inheritance is a flexible template layout that resembles subclasses inheriting the parent class, and the subclass can also
When the changes are made to meet the needs of the current page.
The basic template requires that the Sub-template modification section can be included with <block> and set the name value
<blockname= "Main" > main content </block>
Sub-template as long as the same method, modify <block> internal values to complete the modification
<blockname= "main" > Modified </block>
Create a BASE.TPL template base page under public
<metahttp-equiv= "Content-type" content= "Text/html;charset=utf-8" >
<blockname= "title" ><title>{$user}</title></block>
<body>
<includefile= "Public/header"/>
<blockname= "Main" > main content </block>
<includefile= "Public/footer"/>
</body>
Using the <extend> Import Template base page, name is the path, and the include import method is consistent
<extendname= "Public/base"/>
<blockname= "main" > Modified </block>
Eight Template layout
Thinkphp's template engine has built-in Layout template feature support to facilitate template layout and layout nesting
Function. There are three ways of layout:
1. Global Configuration method
Open the template layout feature and specify the base page
' Layout_on ' =>true,
' Layout_name ' = ' public/layout ',//LAYOUT.TPL file
The base page, {__content__}, replaces the contents of the Quilt page
<includefile= "Public/header"/>
{__content__}
<includefile= "Public/footer"/>
Substitution variable can be changed to {__replace__}
' Tmpl_layout_item ' = ' {__replace__} ',
The sub-template does not need to be loaded into the template base page, you can add {__nolayout__} at the beginning
{__nolayout__}
2. Template label mode
Label mode, do not need to do any configuration in the system, and template inheritance similar to the direct introduction can be.
Sub-template Introduction Template Base page
<layoutname= "Public/layout"/>
Ways to replace variables
<layoutname= "Public/layout" replace= "{__replace__}"/>
3.layout Control Layout
This method is operated in the controller.
Open the layout and introduce the default address base page
Publicfunctionindex () {
Layout (true);
}
Introduce the specified base page
Publicfunctionindex () {
Layout (' Public/layout ');
Layout (false); Shut down
}
Nine Template replacement
Before the template is rendered, the system also carries out some special string substitution operations on the contents of the read template, and implements
The replacement and filtering of the stencil output.
__ROOT__: Will replace the address of the current website (excluding domain name)
__APP__: Will replace the current app's URL address (without domain name)
__MODULE__: Will replace the URL address of the current module (excluding domain name)
__CONTROLLER__ (or __url__ compatible): will replace the current controller's URL address (not
including domain name)
__ACTION__: Will replace the current operation's URL address (without domain name)
__SELF__: Will replace the current page URL
__PUBLIC__: The common directory that will be replaced with the current site is usually/public/
' Tmpl_parse_string ' =>array (
' __public__ ' = '/common ',//change the default/public substitution rules
' __upload__ ' = '/uploads ',//Add new upload path substitution rules
)
Ps:__public__ can be changed to--public--the same can be.

thinkphp--Template Basics

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.