我在互联网上找到了一种用模板构建网站的方法:
if (isset($_REQUEST['page']))
{
if($_REQUEST['page'] !="")
if(file_exists("pages/".$_REQUEST['page'].".html"))
$page_content = file_get_contents("pages/".$_REQUEST['page'].".html");
else
if (file_exists($_REQUEST['page'].".html"))
$page_content = file_get_contents($_REQUEST['page'].".html");
else
echo "<center>Page:".$_REQUEST['page']." does not exist! Please check the url and try again!</center>";
}
else
$page_content = file_get_contents("pages/main.html");
$page_content = str_replace("!!HEADER!!", file_get_contents("design/header.html"),$page_content);
$page_content = str_replace("!!LEFT_COLUMN!!", file_get_contents("design/left_column.html"),$page_content);
$page_content = str_replace("!!RIGHT_COLUMN!!", file_get_contents("design/right_column.html"),$page_content);
$page_content = str_replace("!!FOOTER!!", file_get_contents("design/footer.html"),$page_content);
$page_content = str_replace("!!COMMON_TAGS!!", file_get_contents("design/common_tags.html"),$page_content);
echo $page_content;
对我而言,它看起来还不错,但其目的有点复杂。 我使用这样的东西:
if (isset($_REQUEST["page"])) $page=$_REQUEST["page"];
else $page="main.php";
include 'header.php';
echo "<div id=container>";
echo " <div id=head>";
include 'head.php';
echo " </div>";
echo " <div id=menu-container>";
include 'menu.php';
echo " </div>";
echo " <div id=main>";
include $page;
echo " </div>";
echo "</div>";
include 'footer.php';
是否有理由在第二种方法之上使用第一种方法?