Multiple <head> and <body> tag

前端 未结 3 1616
甜味超标
甜味超标 2021-01-20 19:57

I am trying to create a very simple web application, basically to understand the best practices of coding in HTML5, CSS and JavaScript.

My application has 3-4 pages

相关标签:
3条回答
  • 2021-01-20 20:13

    Another possible solution to avoid multiple head tags which also makes it possible to add additional css files:

    <?php
    // head.php
    $html = <<< EOF
      <!DOCTYPE html>
      <html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en-US">
      <head>
        <title>{$title}</title>
        {$meta}
        <link href="../../css/headermenu.css" rel="stylesheet" type="text/css"/>
        {$additional_style}
      </head>
      <body>
        <ul id="menu">
            <li><a href="#" class="home">Home<span></span></a></li>
        </ul>
        {$mainbody}
        {$footer}
        {$javascript}
      </body>
      </html>
    EOF;
    ?>
    
    <?php
    // page1.php
    
    $title = 'some title';
    $meta = '';
    $additional_style = '';
    $mainbody = 'your body';
    $footer = '';
    $javascript = '';
    
    include_once("head.php");
    echo $html;
    ?>
    
    <?php
    // page2.php
    
    $title = 'some other title';
    $meta = '';
    $additional_style = '<link href="../../css/page2.css" rel="stylesheet" type="text/css"/>';
    $mainbody = 'your body';
    $footer = '';
    $javascript = '';
    
    include_once("head.php");
    echo $html;
    ?>
    

    It also allows for multiple levels of inheritance (for example, you could define the same footer for a couple of pages). This principle can also be used without EOF, but I think that it looks nicer this way.

    The main downside is that you will get a warning if you do not set all the variables of head.php in the pages including it.

    0 讨论(0)
  • 2021-01-20 20:28

    In my opinion it would be a good idea to read about templating systems or have a look how frameworks/CMS handle this.

    Doing it your way, you can't completly avoid repeating e.g. the closing head tag </head> in every content.php.

    So this is just an idea:

    head.php

    <?php
        // Some other includes / requires,
       // code snippets...
    ?>
    <!DOCTYPE html>
    <html>
        <head>
    
            <!-- site-wide stylesheets -->
            <!-- & js-files -->
            <link href="css/main.css" rel="stylesheet" type="text/css"/>
            <script type="text/javascript" src="my/global/scripts.js"></script>
    

    content.php

    <?php
        include ($_SERVER['DOCUMENT_ROOT'] . '/page/common/head.php');
    ?>
    
        <!-- put page specific scripts &     
        <!-- styles here -->
        <link href="my/pages/css/style.css" rel="stylesheet" type="text/css"/>
        <script type="text/javascript" src="my/pages/js/scripts.js"></script>
    </head>
    <body>
        <div id="container">
            <!-- content start -->
            <div id="content">
                <h1>title</h1>
                <p>Your content</p>
            </div>
            <!-- end of content div -->
    <?php
        include ($_SERVER['DOCUMENT_ROOT'] . '/page/common/foot.php');
    

    foot.php

            <div id="foot">
                   copyright etc
            </div> 
        </div> <!-- end of container div -->
    </body>
    </html>
    
    0 讨论(0)
  • 2021-01-20 20:28

    php is rendering html and if you have in both files header of course it will be printed twice

    you should separate in includes but don't write in both files tag

    example

    <header>
    <?php
        // this file should not include <head> taf
        include ($_SERVER[DOCUMENT_ROOT] . '/page/common/header.php);
    ?>
    </header>
    BODY
    <header>
    <?php
        include ($_SERVER[DOCUMENT_ROOT] . '/page/common/foot.php);
    ?>
    </header>
    

    include will bring content from head.php and foot.php and will put in index.php file

    0 讨论(0)
提交回复
热议问题