php框架模板
总览 (Overview)
本文讨论如何为多页网站创建一个简单PHP主模板框架。 模板包含站点的容器,导航,页脚等,即所有常见元素,并根据请求的页面提取特定内容。To make things a bit more intersting lets also base the template on a responsive framework - my preference is foundation so I will use that.
为了使事情更加有趣,让我们也将模板基于响应框架-我的偏好是基础,因此我将使用它。
主模板文件 (The Master Template File)
我们从index.php文件开始。 让我们从一个标准HTML文档开始,并添加一些标记index.php
index.php
<!doctype html>
<html>
<head>
<title>The Title</title>
</head>
<body>
</body>
</html>
To be backwardly compatible with the unmentionable browser we will add the modernizr shim as well.
为了与不易提及的浏览器向后兼容,我们还将添加modernizr填充程序。
Here is a full source listing for a sample index.php file - we will go through it in detail later on in the article.
这是一个示例index.php文件的完整源代码清单-我们将在本文后面的部分中详细介绍它。
index.php
index.php
<?php
require_once('includes/common.php');
$page = empty($_REQUEST['page']) ? 'home' : preg_replace('/[^a-zA-Z0-9\-_]/', '', $_REQUEST['page']);
$pagepath = "includes/$page.class.php";
if (file_exists($pagepath)) {
require_once($pagepath);
$pageclass = $page . 'Class';
}
else {
$pageclass = 'BasePage';
}
$mainframe = new $pageclass($page);
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title><?php $mainframe->title();?></title>
<link rel="stylesheet" href="css/foundation.css" />
<link rel="stylesheet" href="css/custom.css" />
<script src="js/vendor/modernizr.js"></script>
</head>
<body id="<?php echo $page;?>_page">
<div id="wrapper">
<div id="topbar" class="small bottomShadow">
<div class="row">
<div class="large-2 columns"><a href="index.html" id="logo"><img id="logo" src="css/images/logo.png" alt="Experts Exchange"/></a></div>
<div class="large-10 columns" style="margin-top: 10px; text-align: right">
<a href="login.html" class="login">Login</a>
<nav>
<ul>
<li><a href="index.php?page=home">Home</a></li>
<li><a href="index.php?page=aboutus">About US</a></li>
<li><a href="index.php?page=products">Products</a></li>
<li><a href="index.php?page=contact">Contact US</a></li>
</ul>
</nav>
</div>
</div>
</div>
<div class="mainContent">
<!-- CONTENT AREA STARTS HERE -->
<?php $mainframe->render();?>
<!-- CONTENT AREA ENDS HERE -->
</div>
</div>
<div id="footer">
<div class="row">
<div class="large-9 columns">
<div class="row">
<div class="large-4 columns">
<h6>NAVIGATE<h6>
<ul>
<li><a href="/" data-ga-label="Footer" data-ga-action="MasterPage" data-ga-category="HomePage" class="track-click" id="Body_lnkHome">Home</a></li>
<li><a href="/" data-ga-label="Footer" data-ga-action="MasterPage" data-ga-category="HomePage" class="track-click" id="Body_lnkHome">Home</a></li>
<li><a href="/" data-ga