使用php包含外部内容的最佳方法是什么? (对于喜欢最佳实践的Web开发人员)

时间:2021-08-04 18:16:33

I've checked lots of tutorials about including header, navigation and footer using php "include". But I ended up separating my html tags. For instance, my <div id="content"> is in "header.html" while it closing </div> tag is in the footer. This gives me some problem when I use jQuery and looks kinda messy. Is there a better practice to generate external content? (is jQuery the solution?)

我已经使用php“include”检查了很多关于包含标题,导航和页脚的教程。但我最终分离了我的html标签。例如,我的

位于“header.html”,而关闭 标签位于页脚中。当我使用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>

5 个解决方案

#1


What problem is this causing? This is a fairly standard practice (I use it), and I've never known it to create any problems. jQuery shouldn't be an issue, since no jQuery code is ran until all of the html has been joined together anyway.

这造成了什么问题?这是一个相当标准的做法(我使用它),我从来不知道它会产生任何问题。 jQuery应该不是一个问题,因为在所有的html连接在一起之前都没有运行jQuery代码。

#2


Shouldn't be causing any problems. Personally, If navigation was simple, i'd put it inside header, and if not, i'd dynamically generate it via PHP and put it in a class, and include that class in the header.

不应该造成任何问题。就个人而言,如果导航很简单,我会把它放在标题内,如果没有,我会通过PHP动态生成它并将它放在一个类中,并在标题中包含该类。

#3


I do not see a problem with this. I have used this technique many times before. For me it keeps everything well organized. And like Jonathan Sampson said, there should not be any issue with jQuery.

我没有看到这个问题。我以前多次使用过这种技术。对我来说,它保持一切井井有条。和Jonathan Sampson说的一样,jQuery应该没有任何问题。

#4


In my personal opinion this doesn't look right. I mean simply don't do this to yourself. It will be a nightmare if the site becomes more complex. What about only putting the footer div in footer.html the same for header.html? The rest belongs in index.php. If index.php becomes to complex, split it up more (javascript.html..). – merkuro Jun 20 at 21:22

我个人认为这看起来不对。我的意思是根本不要对自己这样做。如果网站变得更加复杂,那将是一场噩梦。仅将footer div中的footer div与header.html相同?其余属于index.php。如果index.php变得复杂,请将其拆分更多(javascript.html ..)。 - merkuro 6月20日21:22

#5


Hm, ok, not sure if this is better way of doing it, but what I tend to do is to have a template file that contains both the header, the footer, the menus, left side, right side, whatever else is not specific to just one page, including all the js includes and such. And then the index.php would simply have a content variable in which all the index content goes. At the end of index i then require the template file which places the "content" variable inside where it belongs and tada, all your headaches are over.

嗯,好吧,不确定这是否是更好的方法,但我倾向于做一个模板文件,包含页眉,页脚,菜单,左侧,右侧,其他任何不具体的只有一页,包括所有js包括等。然后index.php将只有一个内容变量,其中所有的索引内容都是如此。在索引i的末尾,然后需要模板文件将“内容”变量放在它所属的位置和tada中,所有的麻烦都结束了。

#1


What problem is this causing? This is a fairly standard practice (I use it), and I've never known it to create any problems. jQuery shouldn't be an issue, since no jQuery code is ran until all of the html has been joined together anyway.

这造成了什么问题?这是一个相当标准的做法(我使用它),我从来不知道它会产生任何问题。 jQuery应该不是一个问题,因为在所有的html连接在一起之前都没有运行jQuery代码。

#2


Shouldn't be causing any problems. Personally, If navigation was simple, i'd put it inside header, and if not, i'd dynamically generate it via PHP and put it in a class, and include that class in the header.

不应该造成任何问题。就个人而言,如果导航很简单,我会把它放在标题内,如果没有,我会通过PHP动态生成它并将它放在一个类中,并在标题中包含该类。

#3


I do not see a problem with this. I have used this technique many times before. For me it keeps everything well organized. And like Jonathan Sampson said, there should not be any issue with jQuery.

我没有看到这个问题。我以前多次使用过这种技术。对我来说,它保持一切井井有条。和Jonathan Sampson说的一样,jQuery应该没有任何问题。

#4


In my personal opinion this doesn't look right. I mean simply don't do this to yourself. It will be a nightmare if the site becomes more complex. What about only putting the footer div in footer.html the same for header.html? The rest belongs in index.php. If index.php becomes to complex, split it up more (javascript.html..). – merkuro Jun 20 at 21:22

我个人认为这看起来不对。我的意思是根本不要对自己这样做。如果网站变得更加复杂,那将是一场噩梦。仅将footer div中的footer div与header.html相同?其余属于index.php。如果index.php变得复杂,请将其拆分更多(javascript.html ..)。 - merkuro 6月20日21:22

#5


Hm, ok, not sure if this is better way of doing it, but what I tend to do is to have a template file that contains both the header, the footer, the menus, left side, right side, whatever else is not specific to just one page, including all the js includes and such. And then the index.php would simply have a content variable in which all the index content goes. At the end of index i then require the template file which places the "content" variable inside where it belongs and tada, all your headaches are over.

嗯,好吧,不确定这是否是更好的方法,但我倾向于做一个模板文件,包含页眉,页脚,菜单,左侧,右侧,其他任何不具体的只有一页,包括所有js包括等。然后index.php将只有一个内容变量,其中所有的索引内容都是如此。在索引i的末尾,然后需要模板文件将“内容”变量放在它所属的位置和tada中,所有的麻烦都结束了。