PHPLIB Template入门系列 - 4 模板嵌套[3]

[入库:2005年8月19日] [更新:2007年3月24日]

本文简介:选择自 ccterran 的 blog

//将整个文件读进来
$tpl->set_file("main", "third.html");
$tpl->set_file("my_header", "header.html");
$tpl->set_file("my_footer", "footer.html");

//设置header.html里的变量title的值
$tpl->set_var("title", "这个是网页标题");

//设置块
$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");
$tpl->parse("footer", "my_footer");

//完成主模板内变量的替换
$tpl->parse("mains", "main");

//输出
$tpl->p("mains");

?>

输出的结果为
<!-- 这是页面头部 -->
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
<title> 这个是网页标题 </title>
</head>
<body>
下面是一个列表
<ul>

<li>张三 的成绩是 82
<li>李四 的成绩是 90
<li>王二 的成绩是 60
<li>麻子 的成绩是 77
</ul>
<!-- 这是页面脚部 -->
<p>author &copy; iwind
</body>
</html>

一切都是我们所期望的.

在这个程序里,我们用

本文关键:PHPLIB Template入门系列 - 4 模板嵌套
  相关方案
Google
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top