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

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

本文简介:选择自 ccterran 的 blog

<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
<title> </title>
</head>
<body>
下面是一个列表
<ul>
<!-- begin list -->
<li> 的成绩是
<!-- end list -->
</ul>
<!-- 这是页面脚部 -->
<p>author &copy; iwind
</body>
</html>

你会发现,所有的变量都没了,包括我们未赋值的{title},{username},{score}.这是因为我们在创建对象时,第二个参数未设置,而自动采用了"remove"
$tpl = new template("template");

$tpl = new template("template", "remove");
的效果是一样的.

如果我们想给这些变量也赋值,那么方法和单个模板里变量的分析方法是一样的.
//读模板内容进变量
$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);
}

所有程序为
<?php
//包含进模板类 template.inc
require "inc/template.inc";

//创建一个实例
$tpl = new template("template");

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

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

go top