一,Smarty模板技术/引擎——简介

      Smarty是一个使用PHP写出来的模板PHP模板引擎,它提供了逻辑与外在内容的分离,简单的讲,目的就是要使PHP程序员与美工分离,使用的程序员改变程序的逻辑内容不会影响到美工的页面设计,美工重新修改页面不会影响到程序的程序逻辑,这在多人合作的项目中尤为重要。场景一就是登陆之后,跳转到列表页时候,列表页再获取列表数据,如图1。使用smarty后,登陆成功之后,获取列表数据,通过smarty将列表数据发送到列表页,如图2。

                               图1                                                                                                            图2

      在项目中使用smarty,需要引入smarty包,下载smarty,解压缩后,将libs文件夹复制到项目中即可。项目中新建templates和templates_c文件夹,前一个存放模板,后一个存放templates模板对应的编译文件,两个都必不可少。templates里面文件后缀名随意,习惯用tpl和php。项目目录如下图所示,listProcess.class.php代码页此处没有详解。

代码页如下:

login.php

<form action="LoginProcess.php" method="post">
<h1>登陆页面</h1>
用户名:<input type="text" name="username"><br/>
密    码:<input type="password" name="password"><br/>
<input type="submit" value="登陆"><input type="reset" value="重新填写">
</form>

loginProcess.php

<?php
$username = $_POST['username'];
$password = $_POST['password'];

if($username == 'aaa' && $password =='aaa'){
    require_once "listProcess.class.php";
    require_once "./libs/Smarty.class.php";
    $listProcess = new listProcess();
    $res = $listProcess->showEmpList();

    $smarty = new Smarty();
    $smarty->assign("res",$res);
    $smarty->display("empList.php");
//    header("Location:EmpList.php");
}else{
    header("Location:login.php");
}

templates/empList.php

<html>
<h1>显示记录</h1>
<body>
<table>
    <tr><td>id</td><td>name</td><td>password</td></tr>
    {foreach from=$res item=res}
        <tr><td>{$res.empid}</td><td>{$res.name}</td><td>{$res.password}</td></tr>
        {/foreach}
</table>
</body>
</html>

  

原文地址:https://www.cnblogs.com/usa007lhy/p/4958639.html