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>

  

最新文章

  1. OC中用NSSortDescriptor对象进行数组排序
  2. WinForm控件TreeView 只部分节点显示 CheckBox
  3. 【BZOJ-1026】windy数 数位DP
  4. 【POJ3461】Olipo
  5. servlet servlet请求与响应
  6. Crystal Reports &quot;Access to report file denied. Another program may be using it.&quot;
  7. 使用SignalR+Asp.net创建实时聊天应用程序
  8. IOS内存管理「1」- 引用计数
  9. 宏FSP_SEG_INODES_PER_PAGE
  10. object-c 内存管理机制的学习
  11. 程序员之---C语言细节22(函数返回指针注意事项&amp;lt;悬空指针&amp;gt;、查看进程能够分配的内存大小)
  12. 【百度地图API】——如何让标注自动呈现在最佳视野内
  13. 从零开始学C#——基本语法(二)
  14. Nginx平台构架
  15. python标准日志模块logging及日志系统设计
  16. PHP学习笔记2
  17. 蓝牙进阶之路 (003) - AT指令(转)
  18. Redis 操作列表数据
  19. django 加载静态文件(图片,js,css)
  20. Cocos2d-JS实现的2048

热门文章

  1. const&amp;static&amp;extern
  2. zabbix主机自动注册
  3. PythonScripter2.7报错ascii codec can&#39;t encode characters in position 0-1:ordinal not in range(128)
  4. Windows下redis的安装与使用
  5. ECS 游戏架构 理解
  6. Python爬虫实战一之爬取糗事百科段子
  7. vnpy自动化交易
  8. Oracle 11g 重建EM需要删除的对象
  9. Smarty的基本语法------变量调节器
  10. [GO]结构体指针类型匿名字段