Thinkphp template parsing process, research notes.

Source: Internet
Author: User
Tags php template

Thinkphp controller-In the method

$this->display();

To output a view with a template.

Start with this method.

1. Action. Class. php controller base class

This is the controller base class. Find the display () method here.

    protected function display($templateFile='',$charset='',$contentType='',$content='',$prefix='') {        $this->view->display($templateFile,$charset,$contentType,$content,$prefix);    }

We can see that the display () method of the View class is called here.

2. View. Class. php View class

Public function display ($ templatefile = '', $ charset ='', $ contenttype = '', $ content ='', $ prefix = ''){... // parse and obtain the template content $ content = $ this-> fetch ($ templatefile, $ content, $ prefix); // output the template content $ this-> render ($ content, $ charset, $ contenttype );...}

The key lies in the fetch () method and the render () method.

Public Function fetch ($ templatefile = '', $ content ='', $ prefix = ''){... // if no template file is specified, the template file is automatically located, and the page cache ob_start (); ob_implicit_flush (0) is omitted ); if ('php' = strtolower (C ('tmpl _ engine_type '))) {// use the PHP native template // The template array variable to be decomposed into the independent variable extract ($ this-> tvar, extr_overwrite); // directly load the PHP template empty ($ content )? Include $ templatefile: eval ('?> '. $ Content);} else {// view resolution tag $ Params = array ('var' => $ this-> tvar, 'file' => $ templatefile, 'content' => $ content, 'prefix' => $ prefix); tag ('view _ parse', $ Params );} // get and clear the cache $ content = ob_get_clean (); // content filter tag ('view _ filter', $ content); // return $ content in the output template file ;}

To put it simply, $ this-> tvar is an array. When you $ this-> assign ('A', 'B, $ this-> tvar ['a'] = 'B' is automatically added, and then the array is converted to an independent variable through the extract () function, that is, $ A = 'B '; this takes effect only when the PHP native template is used, and we normally use the process after else {, that is

$ Params = array ('var '=> $ this-> tvar, 'file' => $ templatefile, 'content' => $ content, 'prefix' => $ prefix );
Tag ('view _ parse', $ Params );

These two lines of code, $ Params is to make up an array of some variables and call the behavior at view_parse through tags (in CONF/tags. defined in PHP). The behavior class is lib/behavior/parsetemplatebehavior. class. PHP;

This behavior is used to parse view labels.

3. parsetemplatebehavior template parses core Behaviors

The action is parsed using the run () method, and the above $ Params is passed as a reference parameter to run (& $ _ data. That is, $ _ DATA = & $ Params, Which is analyzed from the run () method.
Space relationships. No code will be entered here.

The general process is as follows:

Check whether the template cache file exists and is valid. If it exists, directly include it. If it does not exist, call the fetch method in the thinktemplate class to parse and compile the template, and write the compilation result to the cache, and include cached files.

The cache file is a PHP script that replaces the template with tags. If it is included, the page is output.

4. template compilation

The template compilation function is located in thinktemplate. Class. php. The method is: protected function Compiler ($ tmplcontent) {} has only one parameter, which is to read the content string of the template file.

Protected function Compiler ($ tmplcontent) {// template resolution $ tmplcontent = $ this-> parse ($ tmplcontent ); // restore the replaced literal label $ tmplcontent = preg_replace ('/<! -- ### Literal (\ D +) ### -->/EIS ', "\ $ this-> restoreliteral (' \ 1')", $ tmplcontent ); // Add the security code $ tmplcontent = '<? PHP if (! Defined (\ 'think _ path \ ') Exit ();?> '. $ Tmplcontent; If (C ('tmpl _ strip_space') {/* remove HTML spaces and line breaks */$ find = array ('~> \ S + <~ ',' ~> (\ S + \ n | \ r )~ '); $ Replace = array ('> <','> '); $ tmplcontent = preg_replace ($ find, $ replace, $ tmplcontent );} // optimize the generated PHP code $ tmplcontent = str_replace ('?> <? PHP ', '', $ tmplcontent); Return strip_whitespace ($ tmplcontent );}

Call the parse method parsing template:

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.