您的位置:首页 > 编程语言 > PHP开发

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

2017-10-13 21:06 204 查看
在网上下载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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐