Detailed description of phplib template usage and running principles

Source: Internet
Author: User
Tags php template

Index.htm file content:

<Html>
<Head>
<Body>
<! -- BEGIN RowSort -->
{SortList} <br>
<! -- END RowSort -->
</Body>
</Head>
</Html>

Index. php content:

<?
$ T-> set_file ("Index", "index.htm ");
$ T-> set_block ("Index", "RowSort", "RowsSort ");
........................................ ......
... $ List_sort ...................
$ T-> set_var ("SortList", $ list_sort );
$ T-> parse ("RowsSort", "RowSort", true );
........................................ ......
........................................ ......
$ T-> parse ("OUT", "Index ");
$ T-> p ("OUT ");
?>

Detailed analysis process:

1. $ t-> set_file ("Index", "index.htm"); $ this-> file [index] = index.htm


Ii. $ t-> set_block ("Index", "RowSort", "RowsSort ");

1. Load the index.htm content of the indexfile and assign it to the variable $ this-> varvals [index]
2. $ str = $ this-> varvals [index] Get Template File Content
3. Use the preg_match_all function to match the content in the corresponding block (RowSort) in the template file and store it in $ m [1] [0 ].
4. Why 0? Replace the content (including the header and tail) in the Φ Phi shortlock (RowSort) with {RowSort}
5. Variable $ this-> varvals [RowSort] = $ m [1] [0], $ this-> varkeys [RowSort] =/{RowSort }/
6. Variable $ this-> varvals [index] = $ str (the content has changed, see step 1), $ this-> varkeys [index] =/{index }/


Iii. $ t-> set_var ("SortList", $ list_sort );

Set $ this-> varvals [SortList] = $ list_sort
Set $ this-> varkeys [SortList] =/{SortList }/


4. $ t-> parse ("RowsSort", "RowSort", true); (this operation is repeated based on the sort size)

1. Use the subst function to process all variable values in the $ this-> varvals array against the special characters and $ contained in the values (replace $ with $ and $ respectively, in this way, you can ensure that the next step of preg_replace is not unexpected) and assign it to the $ varvals_quoted array.
2. $ str = $ this-> varvals [RowSort]
3. We can see that $ str contains a {SortList}, which is composed of three: $ this-> varkeys [SortList] =/{SortList }/, $ this-> varvals [SortList] = $ list_sort
4. After preg_replace, we can see that $ str is the content after the PHP template variable {SortList} has been parsed, and $ str is returned.
5. assign a continuation value to $ this-> varvals [RowSort] // you may consider that the {RowSort} was not resolved at the beginning of the display, so don't worry, if phplib has the finish function, it will be processed according to your requirements.


5. $ t-> parse ("OUT", "Index ");

1. As you can see, $ this-> varvals [index] has a PHP template variable to be parsed, that is, {RowSort}
2. In the previous steps, we have resolved the content of $ this-> varvals [RowSort ].
3. In this way, all the PHP template variables have been parsed and assigned to the variable $ this-> varvals [OUT].

6. $ t-> p ["OUT"];

Obviously, the p function in phplib is the output function. We can output $ this-> varvals [OUT] directly.
// Note that the PHP template variable that has not been parsed is automatically executed by phplib.

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.