除了用
php程序传过来的
变量外,另外还可以从配置
文件中读取变量
这在控制整体网站风格上非常方便
下面是本留言板的配置文件gb_config.conf的内容(注意,配置文件要放在configs目录下)
你可自己增加,修改成任何风格
几乎所有的
html的标签属性都可以在这里控制
############ 全局变量 ##########
pageTitle = "smarty留言板v1.0"
vlink="#000000"
link="#000000"
alink="#000000"
text="#000000"
########### 界面风格 ###########
### 风格一: 绿色风格
[greenstyle]
background="#ffffff" #网页背景
daohangbg="images/bg2.jpg"#导航条背景
fpagebg="images/bg2.jpg" #分页背景
tdbg="images/bg2.jpg" #单元格背景
tablebg="#66cc33" #表格背景
linecolor="#669900" #直线color
### 风格二: 蓝色风格
[bluestyle]
background="images/bihaibg.jpg"
daohangbg="images/dh.gif"
fpagebg="images/dh.gif"
tdbg="images/title.gif"
tablebg="#99CCFF"
linecolor="#B4C9E7"
在
模板文件中引用这些变量的方法的例子如下:
注意:在引用之前,一定要有声明(载入配置文件):
<{config_load file="gb_config.conf" section="greenstyle"}>
这里我用的是绿色风格,要用其它风格,修改section即可
(续)
用
smarty打造你的留言板(五)
最后说说撰写的程序gb_write.php
内容更简单,因为根本没用到模板替换变量(除配置文件中的变量外)
<?php
include_once("inc/smarty.inc.php");//载入smarty设置
$smarty -> display('gb_write.tpl');//显示模板
?>
就这么两句,呵呵
模板文件gb_write.tpl的截图
其中
form的action为gb_submit.php(留言提交处理)
代码如下:
<?php
/*==============================================================================
*
* 文 件 名: gb_submit.php
* 程序功能: 留言提交处理
* 更新时间: 2004-08-29
*
* 程序设计: Jzealot
* E-mail :
web-xy@163.com
*
*===============================================================================*/
include_once("inc/gb_conn.inc.php");//载入
数据库连接&请求设置
include_once("gb_error.php");//载入错误处理
页面
//-------------------------------------------------------------------------------
//用户输入检测
//-------------------------------------------------------------------------------
if ( empty($gb_name) )
$errmsg = '-姓名不能为空<br>';
if ( empty($gb_content) )
$errmsg = $errmsg.'-内容不能为空';
if ( !empty($errmsg) ) {
$errmsg = '你的输入有以下错误:<br>'.$errmsg;
throw_err($errmsg);
} else {
//-------------------------------------------------------------------------------
//当输入无错时插入留言到数据库
//-------------------------------------------------------------------------------
mysql_query("insert into guestbook (gb_name, gb_content) values ('".$gb_name."', '".$gb_content."' )");
header("location:gb_list.php");
}
?>
其中gb_error.php也只有一句
<?php
function throw_err($errmsg)
{
require("templates/gb_error.tpl");
}
?>
错误页面模板gb_error.tpl的内容如下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>错误</title>
<link rel="stylesheet" href="css/gb.css">
<style type="text/css">
<!--
.style1 {
font-size: 18px;
font-weight: bold;
color: #FF0000;
}
-->
</style>
</head>
<body>
<table width="416" border="0" align="center" cellspacing="1" bgcolor="#006600">
<tr>
<td width="414" bgcolor="#FFFFFF"><div align="center"><span class="style1">错误!</span></div></td>
</tr>
<tr>
<td bgcolor="#FFFFFF"> <?=$errmsg?></td>
</tr>
<tr>
<td height="20" bgcolor="#FFFFFF"><div align="right"><a href="javascript:history.back();"><-返回</a></div></td>
</tr>
</table>
</body>
</html>
注:在这里用到了一点点php代码(输出错误信息),很遗憾
相关资源:
[smarty实例教学]用smarty打造你的留言板(一)
[smarty实例教学]用smarty打造你的留言板(二)
[smarty实例教学]用smarty打造你的留言板(三)
[smarty实例教学]用smarty打造你的留言板(四)
[
本帖最后由 Ajax_chou 于 2007-7-10 21:37 编辑 ]