调用代码:main.php
<?php
//包含进模板类 template.inc
include "G:/php5/phplib/php/template.inc";
//创建一个实例
$tpl = new Template("./templates/");
//将整个文件读进来
$tpl->set_file("main", "third.html");
$tpl->set_file("my_header", "header.html");
$tpl->set_file("my_footer", "footer.html");
//设置块
$tpl->set_block("main", "list", "lists");
$array = array("张三" => 82, "李四" => 90, "王二" => 60, "麻子" => 77);
foreach ($array as $username=>$score)
{
$tpl->set_var("username", $username);
$tpl->set_var("score", $score);
$tpl->parse("lists", "list", true);
}
//执行my_header,my_footer里的模板变量替换,并把最终结果分别赋给主模板中的header,footer
$tpl->parse("header", "my_header");
//设置header.html里的变量title的值
$tpl->set_var("tt", "这个是网页标题");
$tpl->parse("footer", "my_footer");
//完成主模板内变量的替换
$tpl->parse("mains", "main");
//输出
$tpl->p("mains");
?>
//模板tpl/templates:
1.head.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>xx{tt}yy</TITLE>
</HEAD>
2.footer.html
<p>author iwind</p>
</BODY>
</HTML>
3.
<!-- 这是页面头部 -->
{my_header}
<BODY>
下面是一个列表
<UL>
<!-- BEGIN list -->
<li> {username} 的成绩是 {score}
<!-- END list -->
</UL>
<!-- 这是页面脚部 -->
{my_footer}
<?php
//包含进模板类 template.inc
include "G:/php5/phplib/php/template.inc";
//创建一个实例
$tpl = new Template("./templates/");
//将整个文件读进来
$tpl->set_file("main", "third.html");
$tpl->set_file("my_header", "header.html");
$tpl->set_file("my_footer", "footer.html");
//设置块
$tpl->set_block("main", "list", "lists");
$array = array("张三" => 82, "李四" => 90, "王二" => 60, "麻子" => 77);
foreach ($array as $username=>$score)
{
$tpl->set_var("username", $username);
$tpl->set_var("score", $score);
$tpl->parse("lists", "list", true);
}
//执行my_header,my_footer里的模板变量替换,并把最终结果分别赋给主模板中的header,footer
$tpl->parse("header", "my_header");
//设置header.html里的变量title的值
$tpl->set_var("tt", "这个是网页标题");
$tpl->parse("footer", "my_footer");
//完成主模板内变量的替换
$tpl->parse("mains", "main");
//输出
$tpl->p("mains");
?>
//模板tpl/templates:
1.head.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>xx{tt}yy</TITLE>
</HEAD>
2.footer.html
<p>author iwind</p>
</BODY>
</HTML>
3.
<!-- 这是页面头部 -->
{my_header}
<BODY>
下面是一个列表
<UL>
<!-- BEGIN list -->
<li> {username} 的成绩是 {score}
<!-- END list -->
</UL>
<!-- 这是页面脚部 -->
{my_footer}