Home > php教程 > php手册 > body text

phplib里带的模板功能的确非常好用,但有个致命的小错误需要更正

WBOY
Release: 2016-06-13 11:20:02
Original
797 people have browsed it

    phplib里带的template.inc包含了一个模板类,用它可以实现程序代码和
界面的分离,给程序设计和界面设计带来了极大的方便,而且它的实现比快速
模板要好。但有一个致命的小错误导致set_block()函数不能正常使用,而这个
函数是非常重要的,它在处理重复块时显得非常的好用。
    下面通过一个最简单的例子来讲解用法及解决方法:
模板文件:page1.ihtml(名字你可以随便取,后缀ihtml是一种习惯用法)


{TITLE}



ID: {ID}

NAME: {NAME}






处理文件:test.php
    include("./template.inc");
    # 创建模板变量$t    
    $t = new Template(".", "keep");
    # 定义页面变量的引用,这样你就可以用page1来引用页面了
    $t->set_file("page1","page1.ihtml");
    # 提取名称为TBK的块,让它指向tbks的引用
    $t->set_block("page1", "TBK", "tbks");
    # 替换标题TITLE变量
    $t->set_var("TITLE","my test of template");
    # 定义ID和NAME,然后把TBK附加到tbks
    for ($i=1; $i    $n    = $i;
    $nn = $i*10;
    $t->set_var(array("ID" => $n, "NAME" => $nn));
    $t->parse("tbks", "TBK", true);
    }
    # 最后输出页面
    $t->pparse("out", "page1");

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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template