2012年6月29日 星期五

PHP: 完美實現ASP.NET MasterPage的方法

fate stay night  絕對無敵

喜多郎ASP.NET中的MasterPage實在是太好用了,而在PHP裏面如果隻是用傳統的方法,那麼隻能夠把主要版面分段,然後個別使用include()的方式將主要版面湊出來,總覺得說不出來的彆扭。而且如果切割不好,那麼主要版面一旦改版,那麼內容頁就會改到死。使用傳統方法的程式碼大概像這樣:

<body><div><?php include('menu.php');?></div><div><?php include($page_content);?></div><div><?php include('footer.php');?></div></body>

如果沒有想過這個問題,也不會去找網路資源,原來早就有人找到方法了。主闆頁面就是很一般的頁面,主要用了連個變數$pagetitle和$pagemaincontent,看名字就知道是頁面標題和主要內容。

<?phpdefine('app_root',$_SERVER['DOCUMENT_ROOT']);
require_once(app_root."/shared/opendb.inc");?><html xmlns="http://www.w3.org/1999/xhtml" ><head>    <title><?php echo $pagetitle ?></title>    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />    <link href="./style/globalreset.css" rel="stylesheet" media="screen" type="text/css" />    <link href="./style/layout.css" rel="stylesheet" media="screen" type="text/css" /></head><body>    <div id="page">        <div id="header">            <div id="title">                <h1><a href="./index.php"><img src='./images/logo_full.jpg' height='50' /></a></h1>            </div>            <div id="cart"><?php //require_once(app_root."/shared/login.inc"); ?>                    </div>        </div>        <div id="wrapper">            <div id="sidebar">                <div id="menubox"><?php //require_once(app_root."/shared/navigation.inc"); ?>                        </div>            </div>            <div id="mainbox">                <div id="mainarea"><?php echo $pagemaincontent; ?>                        </div>            </div>        </div>        <div id="footer">        </div>    </div></body></html>

沒有留言:

張貼留言