make header, sidebar & footer constant across webs

2019-09-06 14:16发布

I am programming with Php and i need these elements constant through the web page:
header, sidebar & footer (all php files)

all these files define the respective functions like create_header() and so on that generate the html and i have a css that layout these elements (using div id="header",id="sidebar"..)

as for the pages of actual content all they do is include this files, make the calls and then generate their respective content, all layout through divs, all their content is inside the div class="content"

is it good practice to use this for each page or which method should i use?

3条回答
混吃等死
2楼-- · 2019-09-06 14:45

This is the system that I use when programming with PHP, and it works well for me. It's a good way to ensure consistency, provided that you remember to add the includes to all pages. It also makes updating the layout of the website easier. It's a shame there is no Template or Master Page in PHP, which would be more robust. However, I would strongly recommend HTML's new semantic tags, including <header>, <footer> and <sidebar> instead of giving id's to appropriate <div>'s.

查看更多
劫难
3楼-- · 2019-09-06 14:52

This is what I use for my applications. See if this helps:

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');
?>

This is what my template looks like:

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

This will make header/footer "constant" and dynamic and everything will be defined in terms of constants defined in the site_config.php file. You just need to add appropriate views, static, classes and components directories. See if this helps.

查看更多
一纸荒年 Trace。
4楼-- · 2019-09-06 14:54

include function in PHP is a good way to make the website template constant header and left pane.

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>

Then the foot(bottom) part

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>

Finally we can define the dynamic parts of the web site. I have demonstrated a login window.

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
?>
查看更多
登录 后发表回答