Home > Backend Development > PHP Tutorial > Detailed explanation of the usage process and operating principle of phplib template_PHP tutorial

Detailed explanation of the usage process and operating principle of phplib template_PHP tutorial

WBOY
Release: 2016-07-13 17:36:19
Original
1201 people have browsed it

index.htm文件内容:





{SortList}




index.php内容:

$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");
?>

详细分析过程:

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


二、$t->set_block("Index", "RowSort", "RowsSort");

1、载入index文件index.htm内容并赋给变量$this->varvals[index]
2、$str = $this->varvals[index]获取模板文件内容
3、用preg_match_all函数来匹配模板文件中对应的block(RowSort)里的内容,并存储到$m[1][0]中
4、涯0逦募?卸杂Φ腷lock(RowSort)里的内容(包括头和尾)替换为{RowSort}
5、变量$this->varvals[RowSort] = $m[1][0] ,   $this->varkeys[RowSort] = /{RowSort}/
6、变量$this->varvals[index] = $str(内容已发生改变,见第4步) ,   $this->varkeys[index] = /{index}/


三、$t->set_var("SortList", $list_sort);

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


四、$t->parse("RowsSort", "RowSort", true);(此操作根据sort大小重复进行)

1、利用subst函数把$this->varvals数组中的所有变量值对其中可能含有的特殊字符和$进行处理(和$分别替换为和$,这样可以确保下一步的preg_replace不出现意外),并赋给$varvals_quoted数组
2、$str = $this->varvals[RowSort]
3、我们可以看出此时$str中含有一个{SortList},而由三:$this->varkeys[SortList] = /{SortList}/,$this->varvals[SortList] = $list_sort
4、可见经过preg_replace我们得到的$str是已经解析了PHP模板变量{SortList}之后的内容了,返回$str
5、给$this->varvals[RowSort]进行延续赋值   //这里有个问题大家可能会考虑到就是显示的时候最开始那个{RowSort}未被解析,不用担心,phplib有finish函数会对它根据你的要求进行处理


五、$t->parse("OUT", "Index");

1、可以看出,这时$this->varvals[index]中有一个PHP模板变量要进行解析,即{RowSort}
2、而在前面几步中,我们已经把$this->varvals[RowSort]的内容解析好
3、这样以来,我们把全部PHP模板变量均已解析后的内容赋给变量$this->varvals[OUT]

六、$t->p["OUT"];

显然phplib中p函数即为输出函数,我们直接把$this->varvals[OUT]输出即可
//这里要注意的是输出之前有个phplib自动进行的动作,就是对未被解析的PHP模板变量根据你的要求进行处理.

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/508241.htmlTechArticleindex.htm文件内容: html head body !-- BEGIN RowSort -- {SortList}br !-- END RowSort -- /body /head /html index.php内容: ? $t-set_file ("Index", "index.htm"); $t-set_block...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template