我已将示例网站的 zip 文件上传到 http://superuntitled.com/default/default.zip 如果您喜欢,请查看。
我喜欢将我所有的 html 内容存储在包含的文件中,并让我的 index.php 文件将这些文件调用到 template.php 文件中...
require_once('common.php');
$view = (isset($_GET['page']) && $_GET['page'] != '') ? $_GET['page'] : '';
switch ($view) {
case '' :
$view = 'home';
$section = 'templates/template-default.php';
$pageTitle = "The Homepage";
break;
case 'about' :
$view = 'about';
$section = 'templates/template-about.php';
$pageTitle = "The About Page";
$sidebar = 1;
break;
case 'notfound' :
$section = "templates/template-not-found.php";
$pageTitle = "404 not found";
break;
}
require_once 'view/template.php';
模板文件就这么简单:
require_once('header.php');
if(isset($sidebar))include("view/sidebar.php");
echo "<div id='content'>";
require_once (SERVER_URL.$section);
echo "</div>";
require_once('footer.php');
这样我可以在 index.php 文件中设置各种变量。使用 htaccess (RewriteRule ^([-\A-Za-z0-9\_]*)$ index.php?page=$1),“漂亮的 url”很简单。
common.php 文件包含所有全局变量、会话变量以及数据库连接文件和 php 函数文件。
对于活动类,可以在导航部分检查$view变量,我使用一个简单的php函数:
function curPage($current, $pageTitle) {
if ($pageTitle==$current)
echo " class=\"selected\"";
}
在导航部分我简单地调用这个函数:
<a href="about" <? curPage('about',"$view"); ?> >About</a>
这种方式让事情变得干净和简单。