首页 > 其他 > 详细

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

时间:2015-11-12 13:33:08      阅读:260      评论:0      收藏:0      [点我收藏+]

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

 

技术分享技术分享

                               图1                                                                                                            图2

      在项目中使用smarty,需要引入smarty包,下载smarty,解压缩后,将libs文件夹复制到项目中即可。项目中新建templates和templates_c文件夹,前一个存放模板,后一个存放templates编译文件,两个都必不可少。templates里面文件后缀名随意,习惯用tpl和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";
    $empModel = new listProcess();
$res = $empModel->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>

  

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

原文:http://www.cnblogs.com/usa007lhy/p/4958639.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!