phplib template基础教程核心教程案例
2017-10-13 21:06
204 查看
在网上下载phplib template,在其中找到template.inc单独复制出来导入我们的php文件中就可以使用phplib template
<?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!
<?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
<?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");
?>
<?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>
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<?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>
相关文章推荐
- Spring基础学习教程(Spring之核心容器bean)-06
- [李景山php]每天laravel[023]-laravel 中级任务--小白教程----实际操作-创建基础任务 to-do-list 案例 ---主页,亲爱的,我回来了!!
- Avalon基础视频教程-全选案例问题
- [李景山php]每天laravel[018]-laravel 中级任务--小白教程----实际操作-创建基础任务 to-do-list 案例 ---准备关系模型
- 《Spark商业案例与性能调优实战100课》第18课:商业案例之NBA篮球运动员大数据分析代码实战之核心基础数据项编写
- [李景山php]每天laravel[017]-laravel 中级任务--小白教程----实际操作-创建基础任务 to-do-list 案例 ---准备数据库
- 【备忘】2017年node.js零基础入门到企业级全栈案例开发教程
- Python Django基础教程(二)(模板templatetags)
- 【python学习】网络爬虫——基础案例教程
- Java基础与案例开发详解のjava技术的两个核心
- phplib template简明教程(转)
- laravel 中级任务--小白教程----实际操作-创建基础任务 to-do-list 案例 ---在主页上添加数据
- Docker基础教程——介绍及核心概念
- laravel 中级任务--小白教程----实际操作-创建基础任务 to-do-list 案例 --
- 传智播客Android核心基础课程视频教程(收藏)
- [李景山php]每天laravel[019]-laravel 中级任务--小白教程----实际操作-创建基础任务 to-do-list 案例 ---路由
- phplib template简明教程
- Python基础案例教程
- Android核心基础-6. 演示案例-6.1. 电话拨号
- Python基础案例教程