2014-01-27 23 views
0

簡單的問題在這裏..請問php include文件是否需要重新聲明html標籤?

觀察,我從不同的教程(關於使用包括)..是人們經常重新聲明包含文件中的標籤。

這裏是一個例子。

的index.php

<?php 
include 'header.html.php'; 
include 'content.html.php'; 
include 'footer.html.php'; 

header.html.php

<html> 
<head> All head stuff <head> 
<body> 

content.html.php

<html> 
<head></header 
<body> 
//content 
</body> 
</html> 

footer.html.php

</body> 
</html> 

有沒有包括整個HTML中包括文件格式理由嗎?需要嗎? 謝謝

+2

似乎是一個反模式給我。瀏覽器可以原諒這一點,並顯示預期的輸出(一些瀏覽器可能,至少),但多次包含這些元素仍然是非常不正確的。任何由此不正確標記導致的行爲最多都是未定義的,在調試任何進一步的問題(如CSS/JavaScript故障)之前需要更正。 – David

+0

這是一個解脫..它縫合真的不合邏輯! – user2950370

回答

3

您的content.html.php文件應該而不是有多餘的HTML代碼包含在頁眉和頁腳文件中。事實上,這樣做的目的是爲了讓您擁有共同的頁眉和頁腳代碼,因此您明確不必將其放入內容頁面的主體中。

1

我不知道你在看什麼教程,但他們是錯誤的!看看在瀏覽器中輸出的來源。多個HTML標記是錯誤的。

0

閱讀關於DRY。如果你明白這一點,你會得到你的答案。

我建議你做這樣的:

<!doctype html> 
<html lang="en"> 
<head> 
    <?php include'head.html.php') ?> 
</head> 
<body> 
    <?php include'header_navigation.html.php') ?> 

    <?php include'content.html.php') ?> 

    <?php include 'footer.html.php' ?> 
</body> 
</html> 

head.html.php

<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> 
<title>Your website title</title> 

header_navigation.html.php

<nav> 
    <ul> 
    <li><a href="#">Home</a></li> 
    <li><a href="#">About us</a></li> 
    <li><a href="#">Contact</a></li> 
    </ul> 
</nav> 

內容。 html.php

<section> 
    <article> 
    <h1>Your content title</h1> 
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nihil, quos, saepe a similique totam excepturi enim natus voluptatum rerum eum veritatis aspernatur minus ipsum accusamus voluptate minima quis suscipit officia!</p> 
    </article> 
</section> 

footer.html.php

<!-- your scripts --> 
<script type="text/javascript"> 
    $(document).ready(function() { 

    }); 
</script>