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
2. $t->set_block("Index", "RowSort", "RowsSort");
1. Load the content of the index file index.htm and assign it to the variable $this->varvals[index]
2. $str = $this->varvals[index] gets the 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. Replace the content (including head and tail) in lock (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 4), $this->varkeys[index] = /{index}/
3. $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 according to the size of the sort)
1. Use the subst function to compare all variable values in the $this->varvals array to those that may be contained in it. The special characters and $ are processed (and $ are replaced with and $ respectively, so as to ensure that no accidents occur in the next preg_replace), and assigned to the $varvals_quoted array
2. $str = $this->varvals[RowSort]
3. We can see that $str contains a {SortList} at this time, and from three: $this->varkeys[SortList] = /{SortList}/, $this->varvals[SortList] = $list_sort
4. It can be seen that the $str we get after preg_replace is the content after parsing the PHP template variable {SortList}, and returns $str
5. Perform continuation assignment to $this->varvals[RowSort] // There is a problem here. You may consider that the first {RowSort} is not parsed when displayed. Don’t worry, phplib has a finish function that will handle it according to the Your request will be processed
5. $t->parse("OUT", "Index");
1. It can be seen that there is a PHP template variable in $this->varvals[index] to be parsed, namely {RowSort}
2. In the previous steps, we have parsed the contents of $this->varvals[RowSort]
3. In this way, we assign the parsed contents of all PHP template variables to the variable $this->varvals[OUT]
6. $t->p["OUT"];
Obviously the p function in phplib is the output function , we can directly output $this->varvals[OUT]
//What should be noted here is that there is an action that phplib automatically performs before output, which is to process the unparsed PHP template variables according to your requirements.