根据之前对todolist的了解,我们知道每一个页面都有以及链接中的其中几个
(1)登录
(2)注销
(3)注册
(4)更改密码
(5)返回首页
因此,决定对这几个链接写一个简单的模板。
1.在\Smarty\templates文件夹下建立一个index.html文件
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh-CN">
<head>
<title>{$title}</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<h2 align="center">{$content}</h2>
<a href="{$name1}.php" >{$content1}</a>
<br />
<a href="{$name2}.php" >{$content2}</a>
<br />
<a href="{$name3}.php" >{$content3}</a>
<br />
<a href="welcome.php">返回首页</a>
<br />
</body>
</html>
2.改写之前的Todolist程序
改写后
<?php
session_start();
include("../config.php");
$smarty->assign('title','欢迎页面');
$smarty->assign('content','Todolist,热烈欢迎你!');
if(isset($_SESSION['user_id'])){
$smarty->assign('name1','logout');
$smarty->assign('content1','注销');
$smarty->assign('name2','changepassword');
$smarty->assign('content2','更改密码');
$smarty->assign('name3','list');
$smarty->assign('content3','计划列表');
}else{
$smarty->assign('name1','login');
$smarty->assign('content1','登录');
$smarty->assign('name2','register');
$smarty->assign('content2','注册');
}
$smarty->display('todo.html');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="[http://www.w3.org/1999/xhtml]()" lang="[zh-CN]()">
<head><title>欢迎页面</title>
<meta http-equiv="[Content-Type]()" content="[text/html; charset=utf-8]()" />
</head>
<body> <h2 align="[center]()">Todolist,热烈欢迎你!</h2>
<a href="[login.php](view-source:http://127.0.0.1/sunyan2015/015/login.php)" >登录</a> <br />
<a href="[register.php](view-source:http://127.0.0.1/sunyan2015/015/register.php)" >注册</a> <br />
<a href="[.php](view-source:http://127.0.0.1/sunyan2015/015/.php)" ></a> <br />
<a href="[welcome.php](view-source:http://127.0.0.1/sunyan2015/015/welcome.php)">返回首页</a> <br />
</body>
</html>
改写前
index.php
<?php
session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh-CN">
<head>
<title>欢迎页面</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<h1>欢迎访问todolist</h1>
</body>
</html>
<?php
if(isset($_SESSION['user_id'])){
echo'<a href="logout.php">注销</a>';
echo"<br />";
echo'<a href="changepassword.php">更改密码</a>';
}else{
echo'<a href="login.php">登录</a>';
echo"<br />";
echo'<a href="register.php">注册</a>';
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="[http://www.w3.org/1999/xhtml]()" lang="[zh-CN]()">
<head>
<title>欢迎页面</title>
<meta http-equiv="[Content-Type]()" content="[text/html; charset=utf-8]()" />
</head>
<body>
<h1>欢迎访问todolist</h1>
</body>
</html>
<a href="[login.php](view-source:http://127.0.0.1/sunyan2015/013/login.php)">登录</a><br />
<a href="[register.php](view-source:http://127.0.0.1/sunyan2015/013/register.php)">注册</a>
从改写前,改写后的代码,运行结果和页面源代码,我们看出Smarty模板引擎的运行机制与结果。