【问题标题】:What's the best way to include external content using php includes? (For web developers who love optimal practices)使用 php 包含外部内容的最佳方式是什么? (适用于喜欢最佳实践的 Web 开发人员)
【发布时间】:2009-06-20 20:59:05
【问题描述】:

我查看了很多关于包括页眉、导航和页脚的教程,使用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>

【问题讨论】:

  • 在我个人看来,这看起来不对。我的意思是不要对自己这样做。如果网站变得更加复杂,那将是一场噩梦。仅将页脚 div 放在与 header.html 相同的 footer.html 怎么样?其余的属于 index.php。如果 index.php 变得复杂,则将其拆分更多(javascript.html..)。

标签: php jquery include


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      我认为这没有问题。我以前多次使用过这种技术。对我来说,它让一切井井有条。就像 Jonathan Sampson 所说,jQuery 应该没有任何问题。

      【讨论】:

        【解决方案4】:

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

        【讨论】:

          【解决方案5】:

          嗯,好的,不确定这是否是更好的方法,但我倾向于创建一个包含页眉、页脚、菜单、左侧、右侧等的模板文件不只针对一页,包括所有的js包含等等。 然后 index.php 将简单地有一个内容变量,所有索引内容都在其中。在索引的末尾,我需要将“内容”变量放在它所属的位置的模板文件,tada,你所有的头痛都结束了。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2019-08-22
            • 2011-11-02
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-03-30
            • 1970-01-01
            相关资源
            最近更新 更多