phplib template基础教程核心教程案例

在网上下载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

<?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>










  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值