Php 制作标题、侧边栏和;整个网站的页脚常量
我正在用Php编程,我需要这些元素在网页中保持不变:Php 制作标题、侧边栏和;整个网站的页脚常量,php,css,design-patterns,header,footer,Php,Css,Design Patterns,Header,Footer,我正在用Php编程,我需要这些元素在网页中保持不变: 页眉、边栏和页脚(所有php文件) 所有这些文件都定义了生成html的相应函数,如create_header()等,我有一个css来布局这些元素(使用div id=“header”,id=“sidebar”。) 对于实际内容的页面,他们所做的只是包含这些文件,进行调用,然后生成各自的内容,所有布局都通过div,所有内容都在div class=“content”中 对每个页面使用这个方法是一种好的做法还是应该使用哪种方法?这是我用PHP编程时使
页眉、边栏和页脚(所有php文件) 所有这些文件都定义了生成html的相应函数,如create_header()等,我有一个css来布局这些元素(使用div id=“header”,id=“sidebar”。) 对于实际内容的页面,他们所做的只是包含这些文件,进行调用,然后生成各自的内容,所有布局都通过div,所有内容都在div class=“content”中
对每个页面使用这个方法是一种好的做法还是应该使用哪种方法?这是我用PHP编程时使用的系统,对我来说效果很好。这是确保一致性的一个好方法,前提是您记得将include添加到所有页面。这也使得更新网站布局更容易。遗憾的是,PHP中没有模板或母版页,这将更加健壮。但是,我强烈推荐HTML的新语义标记,包括
、
和
,而不是为相应的
提供id。这是我在应用程序中使用的。看看这是否有帮助:
site_config.php:
<?php
define('DIR_BASE', dirname( dirname(__FILE__) ));
define('DIR_CLASSES', DIR_BASE . '/classes/');
// this is specific to the site
define('DIR_SITE', DIR_BASE . '/yoursite/');
define('SITE_NAME', 'yoursite');
// these would be specific to this site only
define('DIR_SITE_VIEWS', DIR_SITE . 'views/' );
define('DIR_SITE_COMPONENTS', DIR_SITE . 'components/');
define('BASE_URL', 'http://'.$_SERVER['SERVER_NAME']);
define('SITE_URL', BASE_URL . '/yoursite');
// static content for the site
define('DIR_STATIC', SITE_URL . '/static');
define('DIR_JS', DIR_STATIC . '/js');
define('DIR_CSS', DIR_STATIC . '/css');
define('DIR_SITE_IMG', SITE_URL . '/images');
?>
这是我的模板的外观:
<?php
// site_template.php
// template for adding a new page directly under main directory of this site
require_once('site_config.php');
$pgtitle = "Title of the Page";
include_once(DIR_SITE_VIEWS . 'site_header_begin.php');
// custom css/javascript goes here
include_once(DIR_SITE_VIEWS . 'site_header_end.php');
?>
<?php
// main content of the page goes here
?>
<?php
include_once(DIR_SITE_VIEWS . 'site_footer.php');
?>
site_header_begin.php:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>
<?php if (isset($pgtitle) && ($pgtitle != '')){echo $pgtitle." :: ";}?></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="AUTHOR" content="" />
<meta name="KEYWORDS" content="" />
<meta name="DESCRIPTION" content="" />
<!-- common css goes here -->
<script type="text/javascript" language="javascript" charset="utf-8" src="http://code.jquery.com/jquery-1.8.0.min.js"></script>
<script type="text/javascript" src="http://code.jquery.com/ui/1.8.17/jquery-ui.min.js"></script>
site_header_end.php:
</head><!-- end the header here -->
<body ><!-- start the body here -->
site_footer.php:
<div id="footer"><a name="pageFooter"></a>
</div>
</body>
</html>
这将使页眉/页脚成为“常量”和动态的,所有内容都将根据site_config.php文件中定义的常量进行定义。您只需要添加适当的视图、静态、类和组件目录。看看这是否有帮助。PHP中的include函数是使网站模板保持不变的好方法 标题和左窗格
in1.php(此文件包含常量接口的顶部)
最后,我们可以定义网站的动态部分。我演示了一个登录窗口
login.php
<?php
include 'in1.php'; //previously made top part
?>
<!--start of the dynamic main pane-->
<div id="rightd" >
<form name="login" action="user.php" method="post">
<table>
<tr><td>Email: </td><td colspan="2" ><input type="text" name="email" size="30" required/></td></tr>
<tr><td>password: </td><td colspan="2"><input type="text" name="pass" size="30" required/></td></tr>
<tr><td></td><td><input type="submit" value="OK" name="log"/><input type="reset" value="clear" /></td></tr>
</table>
</form>
<?php
//a php code here:
?>
</div>
<!--end of the dynamic main pane-->
<?php
include 'in2.php'; //previously made footer part
?>
电邮:
密码:
我认为一个常见的做法是创建一个名为“namespace.php”的文件,并将所有常用的include都放在其中,然后在每个页面中只需包含一个文件,这会让事情变得更简单,谢谢这很好,但是我的许多潜在用户仍然使用IE 8,它不支持那些专门的标记来回答问题,但我没有“得到定义('DIR_STATIC',SITE_URL.'/STATIC');,为什么/static,还有DIR_站点可以是ww.mysite.com,对吗?如果不需要,你不必把它们放在那里。它们是特定于我的站点的,因为我将所有js/css/img文件存储在一个静态目录中。此外,DIR_站点允许您包含/需要文件,而不需要它们的绝对路径。此代码与服务器无关。您只需要对site_config.php文件进行更改。你可能需要根据自己的需要对其进行一些定制。好的,非常感谢现在我知道了一个构建网站的好方法。当然。了解这是许多可能的方法之一。您可以从这个开始,然后根据需要定制它。
<!--start of the footer-->
<div id="foot" style="">
<center class="footfix">
©KS <a href="http://ugvle.ucsc.cmb.ac.lk">visit us</a>
</center>
</div>
<!--end of the footer-->
<div></div>
</body>
</html>
<?php
include 'in1.php'; //previously made top part
?>
<!--start of the dynamic main pane-->
<div id="rightd" >
<form name="login" action="user.php" method="post">
<table>
<tr><td>Email: </td><td colspan="2" ><input type="text" name="email" size="30" required/></td></tr>
<tr><td>password: </td><td colspan="2"><input type="text" name="pass" size="30" required/></td></tr>
<tr><td></td><td><input type="submit" value="OK" name="log"/><input type="reset" value="clear" /></td></tr>
</table>
</form>
<?php
//a php code here:
?>
</div>
<!--end of the dynamic main pane-->
<?php
include 'in2.php'; //previously made footer part
?>