简体   繁体   English

插入兼容HTML5的外部html内容的最佳方法? php vs HTML5 vs javascript方法

[英]Optimum way to insert external html content HTML5 compliant? php vs HTML5 vs javascript methods

I know how simple this probably seems to you gurus, but I have been searching for over an hour to no avail... 我知道这对您来说似乎很简单,但是我一直在搜索一个多小时,但无济于事。

Goal: Use a single footer file and menu file for all my webpages. 目标:对我所有的网页都使用一个页脚文件和菜单文件。 Taking into account blocking, speed, etc. The content of my menu is pure html/css and the content of my footer is pure html/css. 考虑到阻塞,速度等。菜单的内容为纯html / css,页脚的内容为纯html / css。 Would the optimal solution change based on the content being injected? 最佳解决方案会根据注入的内容而改变吗? eg If videos, jscript, etc. were involved. 例如,如果涉及视频,jscript等。

Two part question: 1) Which method is optimal? 两部分问题:1)哪种方法是最佳的? Some kind of php include, using the tag, using jscript, etc. 2) How precisely is this achieved keeping HTML 5 standards? 某些php包括使用标记,使用jscript等。2)保持HTML 5标准的精度如何? ie For the php method to work, does my calling webpage need to be .php and then does that make the HTML5 standard a moot point? 即,要使php方法起作用,我的调用网页是否需要为.php,然后使HTML5标准成为争议点吗? eg If I want to inject footer.php into index.html, does my index file also have to be .php? 例如,如果我想将footer.php注入index.html,我的索引文件也必须是.php吗? Similarly for the tag, can the external file be an .html file(I don't like the idea of reloading all the header information with .css calls) or should it be .php? 同样,对于标签,外部文件可以是.html文件(我不喜欢通过.css调用重新加载所有标头信息的想法)还是应该是.php?

Within the index.html file I have tried the following: 在index.html文件中,我尝试了以下操作:

<object id="footerArea" width="100%" height="20%" 
  type="text/html" data="footer.html">
</object>

and

<?php include 'footer.php' ?>

Neither of these seem to work for me. 这些似乎都不适合我。

In case you are wondering... Here is the code for my footer I am trying to inject with sample data to make it shorter and easier to read: 如果您想知道...这是我的页脚代码,我正尝试注入示例数据以使其更短,更易于阅读:

<div class="footer box">
<p class="f-right t-right">
 <a href="#">www.mysite.com</a><br />
  Address: Medford, OR<br />
  Phone: (541) 555-5555
</p>

<p class="f-left">
 Copyright &copy;&nbsp;2011 <a href="#">My Name</a><br />
</p>

<p class="f-left" style="margin-left:20px;">
 <a href="http://sampleurl.com" target="_blank">
  <img style="border:0;width:88px;height:31px"
   src="http://sampleurl.com"
   alt="Valid CSS3!" />
 </a>
</p>

<p class="f-left" style="margin-left:20px;">
 <a href="http://sampleurl" target="_blank">
 <img src="http://sample.png" width="228" height="50" alt="sample alt" title="sample title">
 </a>
</p>
</div>

Please excuse my formatting. 请原谅我的格式。 I am still new to posting code in forums. 我对在论坛中发布代码还是陌生的。 I tried my best :) 我已经尽力了 :)

The extension of a filename you seen in a url has absolutely NOTHING with how that file will be treated by a browser when it's downloaded. 您在url中看到的文件名扩展名与下载该文件时浏览器将如何对待它完全无关 It all comes down to the Content-type header that accompanies the file. 全部归结为文件随附的Content-type标头。 A webmaster can trivially configure their server to treat all .exe files as plain HTML pages. 网站管理员可以简单地将其服务器配置为将所有.exe文件视为纯HTML页面。 They can also tell the webserver to run .html pages through the PHP parser. 他们还可以告诉Web服务器通过PHP解析器运行.html页面。 In fact, with "modern" SEO-optimized urls, you rarely see a file extension at all. 实际上,使用“现代” SEO优化的URL,您几乎看不到文件扩展名。 It'll all be things like example.com/some/wonky/path , not example.com/page.php?id=wonky . 都是example.com/some/wonky/path东西,而不是example.com/page.php?id=wonky类的东西。

The fact that PHP has built and output a page also has nothing to do with HTML compliance. PHP已构建并输出页面的事实也与HTML兼容性无关。 It comes down to whether the page the browser receives conforms to the standards. 这取决于浏览器接收的页面是否符合标准。 Are all tags properly closed? 所有标签是否都正确关闭? Attributes properly defined? 正确定义的属性? Tags properly nested? 标签是否正确嵌套? Blah blah blah. 等等等等等等。

If you've built your code properly, the html that's output will be properly structured and be valid html. 如果您正确构建了代码,则输出的html将被正确地结构化并成为有效的html。 If it's not valid html, that's not PHP's fault - that's your fault for putting together code that doesn't produce the proper output. 如果它不是有效的html,那不是PHP的错-这是您将无法产生正确输出的代码放在一起的错。

The only time a file extension in a URL MIGHT be relevant is if the webserver outputs a generic content-type, eg "application/octet-stream". URL MIGHT中唯一与文件扩展名相关的时间是Web服务器是否输出通用的内容类型,例如“ application / octet-stream”。 The browser MAY use a detectable file extension to guess at the content's type and try to treat it as such. 浏览器可以使用可检测的文件扩展名来猜测内容的类型,并尝试将其视为此类。 But this is not guaranteed nor reliable. 但这不能保证也不可靠。

This is what a PHP include should look like: 这就是PHP包含的内容

<?php include 'footer.php'?>

As far as I can see the code you have in your question is assigning the string "footer.php" to the variable include . 据我所见,您所遇到的代码是将字符串“ footer.php”分配给变量include However, rather than rolling your own template system, have you considered using something like Smarty ? 但是,您是否考虑使用Smarty之类的东西,而不是滚动自己的模板系统?

If the called code like for a footer that is canned, you might want to create the simple footer like you want then include: 如果所调用的代码像固定的页脚一样,则可能需要创建所需的简单页脚,然后包括:

<?php
readfile("yourfile.htm");
?>

Something like the following should do what you want: 像下面这样的事情应该做你想要的:

index.php 的index.php

<html>
  <head>
    <link rel="stylesheet" type="text/css" href="main.css" />
    <!-- other css, scripts etc -->
  </head>
  <body>
    <!-- your main content here -->
    <?php include 'footer.html' ?>
  </body>
</html>

footer.html footer.html

<div class="footer box">
  <!-- your footer content here -->
</div>

when you load index.php in the browser, and "view source", you should see the contents shown above, but the <?php include "footer.html" ?> should be replaced with the content from the file "footer.html". 在浏览器中加载index.php和“查看源代码”时,应该看到上面显示的内容,但是<?php include "footer.html" ?>应该替换为文件“ footer.html”中的内容”。

You should not see the php code itself when you view source through the web-browser. 应该看到的PHP代码本身,当你通过网络浏览器查看源代码。 If you do see php code in "view source", this indicates that your server isn't configured to run php properly. 如果您确实在“查看源代码”中看到php代码,则表明您的服务器未配置为正确运行php。


For an alternate approach, which loads the content from the browser, and which doesn't use php, I'll point you to this related question and answer . 对于从浏览器加载内容并且不使用php的另一种方法,我将向您指出相关的问答

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM