在网上下载phplib template,在其中找到template.inc单独复制出来导入我们的php文件中就可以使用phplib template
1 简单变量替换
t.php
<?php
include ("template.inc");
$t = new template(".", "keep");
$t->set_file("gg", "gg.html");
$t->set_var("title","good boy!");
$t->parse("ggp","gg");
$t->p("ggp");
?>
gg.html
<!DOCTYPE html>
<html>
<head>
<title>{title}</title>
<meta charset="utf-8">
</head>
<body>
<p>-----------</p>
<!-- BEGIN li -->
<p>{name}</p>
<!-- END li -->
<p>-----------</p>
</body>
</html>
运行t.php可以看到{title}被替换为good boy!
2 block变量替换
t.php
<?php
include ("template.inc");
$t = new template(".", "keep");
$t->set_file("gg", "gg.html");
$t->set_var("title","good boy!");
$t->set_block("gg", "li", "nli");
$t->set_var("name", "xiaoming");
$t->parse("nli", "li", true);
$t->parse("ggp","gg");
$t->p("ggp");
?>
gg.html
<!DOCTYPE html>
<html>
<head>
<title>{title}</title>
<meta charset="utf-8">
</head>
<body>
<p>-----------</p>
<!-- BEGIN li -->
<p>{name}</p>
<!-- END li -->
<p>-----------</p>
</body>
</html>
运行t.php可以看到{name}被替换为xiaoming
3 block变量循环替换
t.php
include ("template.inc");
$t = new template(".", "keep");
$t->set_file("gg", "gg.html");
$t->set_var("title","good boy!");
$t->set_block("gg", "li", "nli");
$i = 0;
while ( $i <= 3) {
$t->set_var("name", "xiaoming".$i++);
$t->parse("nli", "li", true); //这里的true不要掉了
}
$t->parse("ggp","gg");
$t->p("ggp");
?>
4 用其他模板替换变量(模板变量)
t.php
<?php
include ("template.inc");
$t = new template(".", "keep");
$t->set_file("gg", "gg.html");
$t->set_file("h", "header.html");
$t->parse("header", "h");
$t->set_file("f", "footer.html");
$t->parse("footer","f");
$t->parse("ggp","gg");
$t->p("ggp");
?>
header.html
<p>xiaoming is a header</p>
footer.html
<p>xiaoyang is a footer</p>
gg.html
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="utf-8">
</head>
<body>
{header}
{footer}
</body>
</html>