Smarty模板技术/引擎——简介
2015-11-12 12:45
309 查看
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
loginProcess.php
templates/empList.php
图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>
相关文章推荐
- 学习日记
- java.awt.Robot
- java中注解的使用与实例 (二)
- JD-GUI查看jar包里java源码
- 去掉中文空格的PHP正则代码
- java.awt.Robot
- 一个图形的算法网站: http://visualgo.net/
- Struts2 的国际化实现
- JAVA计算器
- Swift对面向对象提供了良好的支持,下面介绍几个其独有的特性。
- 图标库网络地址
- 使用emguCV/C# opencv绘制自定义直方图
- 习近平与“十三五”十四大战略:网络强国战略
- C语言输入终止
- TabBar滑动效果
- PBR:应用于虚幻引擎4贴图和材质创建的启示
- C++ primer plus 第六版中文版 18.3
- 《JAVA---day03---运算符》
- 以HTTL为例讲讲模块分包&领域模型&扩展框架
- redis常用命令