簡體   English   中英

使用php include包含外部內容的最佳方法是什么? (對於喜歡最佳做法的Web開發人員)

[英]What's the best way to include external content using php includes? (For web developers who love optimal practices)

我檢查了很多有關使用php“ include” 包括頁眉,導航和頁腳的教程 但是我最終分離了我的html標簽。 例如, my <div id="content">位於“ header.html”中,而它的</div>標記位於頁腳中。 當我使用jQuery時,這給了我一些問題,看起來有點混亂。 是否有更好的做法來生成外部內容? (jQuery是解決方案嗎?)

index.php

<?php include("header.html"); ?>
<?php include("navigation.html"); ?>
<div id="content">
    <h2 class="clear">This is the contact page</h2>
        <p>
        Main content here... Main content here... Main content here... Main content here...
        Main content here... Main content here... Main content here... Main content here...
        Main content here... Main content here... Main content here... Main content here...
        Main content here... Main content here... Main content here... Main content here...
        Main content here... Main content here... Main content here... Main content here...
        Main content here... Main content here... Main content here... Main content here...
        </p>
    </div>
<?php include("footer.html"); ?>

header.html

<!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>
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
    <title>Study at Best</title>
    <script type="text/javascript" src="scripts/jquery-1.3.2.min.js"></script>
    <script type="text/javascript" src="scripts/jquery.corner.js"></script>
    <script type="text/javascript" src="scripts/jquery.dropshadow.js"></script>
    <script type="text/javascript" src="scripts/jqueryScripts.js"></script>
    <link rel="stylesheet" rev="stylesheet" href="styles/layout.css" />
    <link rel="stylesheet" rev="stylesheet" href="styles/ddm.css" />
</head>
<body>
<div id="container">
    <div id="header">
    This is the header
    </div>

footer.html

    <div id="footer">
        <p>BEST School &copy; Copyright 2009</p>
    </div>
</div>
</body>
</html>

這是什么問題? 這是一個相當標准的做法(我使用它),但我從不知道會造成什么問題。 jQuery不應成為問題,因為直到所有html都連接在一起之前,就不會運行jQuery代碼。

不應該引起任何問題。 就個人而言,如果導航很簡單,我會將其放在標題中,否則,我將通過PHP動態生成它並將其放在一個類中,並將該類包含在標題中。

我沒有看到這個問題。 我以前使用過這種技術很多次。 對我來說,它使一切井井有條。 就像喬納森·桑普森(Jonathan Sampson)所說的那樣,jQuery應該沒有任何問題。

我個人認為這不正確。 我的意思是根本不要對自己這樣做。 如果站點變得更加復雜,那將是一場噩夢。 只將footer.html中的footer div與header.html相同怎么辦? 其余的屬於index.php。 如果index.php變得復雜,請將其拆分成更多(javascript.html ..)。 – merkuro 6月20日21:22

嗯,好的,不確定這是否是更好的方法,但是我傾向於做的是創建一個包含頁眉,頁腳,菜單,左側,右側以及其他未指定內容的模板文件到一頁,包括所有js包含的內容等等。 然后index.php將只具有一個內容變量,其中所有索引內容都存儲在其中。 然后在索引末尾,我需要模板文件,該模板文件將“ content”變量放置在它所屬的位置和tada中,您的所有煩惱都過去了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM