利用Smarty模板技术的一个小案例

stu.php


<?php
include("connect.php");//引进数据库连接文件
include("../libs/Smarty.class.php");//引入Smarty源文件
$smarty = new Smarty();
$smarty->caching = 1;
$smarty->InitSmarty("../demo/templates","../demo/templates_c","../demo/configs","../demo/cache");//设置储存路径,配置文件路径,缓存文件路径
if(!$smarty->is_cached("stu.tpl")){
$q = "select * from stu";
$result = mysql_query($q);
$i = 0;
$array = array();
while($row = mysql_fetch_assoc($result)){
$array[$i] = $row;
$i++;
}
$smarty->assign("array",$array);
echo "当前模板没有被加载";
}
$smarty->display("stu.tpl");
?>








stu.tpl


<{config_load file="a.conf"}>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>


<body>
<center>
<table bgcolor="<{#table_bgcolor#}>" >
<caption>学生基本信息</caption>
<tr bgcolor="<{#head_color#}>">
<th>学号</th>
<th>姓名</th>
<th>出生日期</th>
<th>学分</th>
<th>操作</th>
</tr>
<{section name=stu loop=$array}>
<{if $smarty.section.stu.index is odd}>
<tr bgcolor="<{#row_color1#}>">
<td><{$array[stu].id}></td>
<td><{$array[stu].name}></td>
<td><{$array[stu].birthday}></td>
<td><{$array[stu].score}></td>
<td><a href="details.php?id=<{$array[stu].id}>" >详细信息</a></td>
</tr>
<{else}>
<tr bgcolor="<{#row_color2#}>">
<td><{$array[stu].id}></td>
<td><{$array[stu].name}></td>
<td><{$array[stu].birthday}></td> 
<td><{$array[stu].score}></td>
<td><a href="details.php?id=<{$array[stu].id}>" >详细信息</a></td>
</tr>
<{/if}>
<{/section}>
</table>
</center>
</body>
</html>






details.php


<?php
include ("connect.php");//引进数据库连接文件
include("../libs/Smarty.class.php");//引入Smarty源文件
$smarty = new Smarty();
$smarty->caching=1;
$smarty->InitSmarty("../demo/templates","../demo/templates_c","../demo/configs","../demo/cache");//设置储存路径,配置文件路径,缓存文件路径


if(!$smarty->is_cached("stu_detail.tpl",$_GET["id"])){
$q = "select * from stu where id = ".$_GET["id"];
$result = mysql_query($q);
$array = array();
$i=0;
while($row = mysql_fetch_assoc($result)){
$array[$i]=$row;
$i++;
}
$smarty->assign("stu_details",$array);
echo "缓存没有被设置。。";
}


$smarty->display("stu_detail.tpl",$_GET["id"]);
?>






stu_detail.tpl


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>学生详细信息</title>
</head>


<body>
<{section name=stu_detail loop=$stu_details}>
<h1>
<{$stu_details[stu_detail].name}>的详细信息
</h1>
学号:<{$stu_details[stu_detail].id}><br />
姓名:<{$stu_details[stu_detail].name}><br />
出生日期:<{$stu_details[stu_detail].birthday}><br />
<{/section}>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值