39th Chapter thinkphp--Template Foundation

Source: Internet
Author: User

Learning points: 1. Variable output 2. System Variables 3. Use the function 4. Use operator 5. include file 6. Template Notes 7. Template Inheritance 8. Template Layout 9. Template replacement

In this lesson, we are going to learn about the thinkphp template, the thinkphp built-in XML-based performance of the template engine Thinktemplate, using dynamic compilation and caching technology to support the custom tag library. A Variable output is very easy to output variables in the template, using the Assign () method, passing variables and values in the form of key-value pairs. Pass a variable to the template $user = ' crayon small new '; $this->assign (' user ', $user); You can call this in the template: {$user} The result of the template compilation is: You can view the template label {and $ without any spaces between Runtime, otherwise it cannot be resolved. If you want to change two {} can be set://Modify the default label ' tmpl_l_delim ' = ' <{', ' tmpl_r_delim ' = '}> ', the calling method must be changed: <{$user}> If an array is passed , we pass through the past, call through two ways://To the template passed an array $data [' user '] = ' crayon small new '; $data [' email '] = ' [email protected] '; $this->assign (' data ', $data); Called by: user:{$data. User} email:{$data. email} user:{$data [' User ']} email:{$data [' Email '} If you pass an object, we pass the past directly, Call in two ways://Pass an object to the template $data = new \stdclass (); $data->user = ' crayon small 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, not only can output PHP system variables, you can also output thinkphp system variables. Output PHP system variable {$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 ']//thinkphp system variable {$Think. Const.app_path}//directory {$Think. Config.url_model}//url Mode {$Think. Lang.var_error}//language variable three. Using functions if we need to use PHP functions in the template at times, we can use the following format: {$user |MD5} If you have multiple parameters to pass, you can refer to the following settings: {$date |date= "y-m-d h:i:s", # #} PS: Indicates date Function passed two parameters, each parameter is separated by commas, here the first argument is y-m-d h:i:s, the second argument is the date variable to be output before, because the variable is the second parameter, so you need to use # # # to identify the variable location, compiled result is: The preceding output variable, defined later, does not require # # # {$user |mb_substr=0,3, ' UTF-8 '} multiple functions with ' | ' Can be separated {$user |mb_substr=0,3, ' UTF-8 ' |MD5|SHA1} PS: If you think the above writing needs to be translated in the mind two times, too much trouble, then you may use the following format notation: {: 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 must be on the template side to deal with. 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 operators in templates, including support for "+", "-", "*", "/", "%", "--" and "+ +". {$data [' num ']+10}//brackets {$data [' num ']+getnum ()}//function PS: syntax and general function usage are no longer supported when using operators: {$data. num+10}//error {$data [' num ']+ Getnum}//The error template also supports ternary operators: {$user? ' Value ': ' No value '}

Five Include files in a system that can contain generic header files and foot files: header and footer. Since each page has the same head foot, it needs to be detached separately and introduced into them with the inclusion file. We can create a public directory under the default theme directory in the View directory, which is dedicated to common call template files. You can also call the absolute path pattern: Multiple template files are called simultaneously:

Six. template annotation Template support annotation function, provide a template for the production staff reference. {//This is a comment} {/* This is also a comment */} {/* This is a multiline comment */} seven. Template inheritance template inheritance is a flexible template layout, similar to subclasses inheriting the parent class, and the subclass can also make appropriate modifications to meet the needs of the current page. The basic template needs to modify the sub-template section can be included, and set the name value of the main content//sub-template as long as the same method, modify the internal values can be modified//create Public BASE.TPL template Base page main content//Use the Import Template base page, name is the path, And the include import method have been modified uniformly by eight. Template layout thinkphp's template engine has built-in support for layout templates, which makes it easy to implement template layouts and layout nesting functions. There are three ways of layout: 1. Global configuration Mode//Open template layout function, and specify the base page ' layout_on ' =>true, ' layout_name ' + ' public/layout ',//layout.tpl file//Base page, {_ _CONTENT__} Replace the contents of the quilt page with the {__content__}//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__} {__nolayout__} 2 at the beginning. Template label mode, do not need to do any configuration in the system, and template inheritance similar, directly introduced can. Sub-template Introduction Template base page//replacement variable method 3.layout control Layout This method is operated in the controller. Opens the layout and introduces the default address base page Public Function index () {layout (true);}//introduces the specified base page Public Function index () {layout (' public/layout ');//l Ayout (FALSE); Close}

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 substitution and filtering of the template output. __ROOT__: will replace the current website address (without domain name) __app__: will replace the current app's URL address (without the domain name) __module__: will replace the current module's URL address (without the domain name) __controller__ (or __url__ and The current controller's URL address (without the domain name) __action__: will replace the current operation's URL address (without the domain name) __self__: will be replaced with the current page URL __public__: The public directory that will be substituted for the current site is usually/p ublic/' tmpl_parse_string ' =>array (' __public__ ' = '/common ',//change the default/public substitution rule ' __upload__ ' = '/uploads ',/ /Add new upload path substitution rules) ps:__public__ can be changed to--public--the same can be.

39th Chapter thinkphp--Template Foundation

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.