【问题标题】:make header, sidebar & footer constant across website使整个网站的页眉、侧边栏和页脚保持不变
【发布时间】:2013-07-22 19:16:01
【问题描述】:

我正在使用 Php 进行编程,我需要这些元素在网页中保持不变:
页眉、侧边栏和页脚(所有 php 文件)

所有这些文件都定义了相应的函数,如 create_header() 等生成 html,我有一个布局这些元素的 css(使用 div id="header",id="sidebar"..)

对于实际内容的页面,他们所做的只是包含这些文件,调用然后生成它们各自的内容,所有的布局都是通过divs,它们的所有内容都在div class="content"

在每个页面上使用它是一种好习惯吗?或者我应该使用哪种方法?

【问题讨论】:

  • 我认为一种常见的做法是创建一个名为“namespace.php”的文件并将所有常用的包含在其中,然后您只需在每个页面中包含一个文件
  • 这让事情变得更容易,谢谢

标签: php css design-patterns header footer


【解决方案1】:

这是我在使用 PHP 编程时使用的系统,它对我来说效果很好。这是确保一致性的好方法,前提是您记得将包含添加到所有页面。它还使更新网站的布局更容易。遗憾的是 PHP 中没有模板或母版页,这会更健壮。但是,我强烈推荐 HTML 的新语义标签,包括 <header><footer><sidebar>,而不是给适当的 <div> 提供 id。

【讨论】:

  • 这很好,但我的许多潜在用户仍然使用不支持那些专用标签的 IE 8
【解决方案2】:

这是我用于我的应用程序的。看看这是否有帮助:

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 文件中定义的常量进行定义。您只需要添加适当的视图、静态、类和组件目录。看看这是否有帮助。

【讨论】:

  • 感谢您的回答,但我没有定义('DIR_STATIC', SITE_URL . '/static');,为什么 /static 和 DIR_SITE 可以是 wwww.mysite.com 对吗?
  • 如果你不需要的话,你不必把它们放在那里。它们特定于我的站点,因为我将所有 js/css/img 文件存储在静态目录中。
  • DIR_SITE 也将允许您包含/需要文件,而无需它们的绝对路径。此代码与服务器无关。您只需要更改 site_config.php 文件。您可能需要根据需要对其进行一些自定义。
  • 好的,非常感谢,现在我知道了一种构建网站的好方法。
  • 当然。了解这是许多可能的方法之一。您可以从这个开始,然后根据您的需要对其进行自定义。
【解决方案3】:

PHP 中的include 函数是使网站模板保持不变的好方法 标题和左窗格。

in1.php (this file contains top part of the constant interface)

<?php
//session maintenance code:
?>

<!DOCTYPE HTML>
<html>
<head>
    <title>Admin interface</title>
    <link rel="stylesheet" type="text/css" href="../css/pageBody.css" />
    <link rel="stylesheet" type="text/css" href="../css/dropdown.css"/>

</head>
<body>
    <!--top divisor-->
     <div id="topp">
        <center>
            top pane content should be coded here
        </center>
    </div>
    <!--end of the top divisor-->

    < div id="bottom">
         <!--start of the left pane-->
         <div id="leftd" >
            <P>
                 left pane content should be coded here:
            </P>
        </div>

然后是脚(底部)部分

in2.php (this file contains the constant end part of the web site)

    <!--start of the footer-->
    <div id="foot" style="">
        <center class="footfix">
            &copy;KS <a href="http://ugvle.ucsc.cmb.ac.lk">visit us</a>
        </center>
    </div>
    <!--end of the footer-->

    <div></div>
 </body>

</html>

最后我们可以定义网站的动态部分。我已经演示了一个登录窗口。

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
?>

【讨论】:

    猜你喜欢
    • 2015-08-22
    • 1970-01-01
    • 2013-03-16
    • 2010-12-14
    • 2016-06-26
    • 1970-01-01
    • 1970-01-01
    • 2023-03-16
    • 2013-02-21
    相关资源
    最近更新 更多