在页面中包含 HTML 片段 - 方法?
这是我之前提出的问题的扩展,在这里:
Django - 解析 XML,输出为 iFrame 的 HTML 片段?
基本上,我们正在考虑将各种 HTML 片段集成到页面中。我们有一个小型网络应用程序,可以在不同的 URL 上为不同的结果/小工具生成小片段。这不一定与主页位于同一域中。
我想知道将这些内容包含到主页中的最佳方式是什么?我们还需要能够使用主页中的 CSS 为 HTML 片段换肤。
我最初的想法是 iFrames,但是,我认为它的性能可能不是很好,并且对所包含片段的 CSS/JS 操作有限制。
SSI 是一个更好的主意吗?或者我们应该使用 php include 吗?还是JS?还有其他建议吗?两个主要考虑因素是主页的性能以及设计/操作所包含片段的能力。
干杯, 胜利者
This is an extension of an earlier questions I asked, here:
Django - Parse XML, output as HTML fragments for iFrame?
Basically, we're looking at integrating various HTML fragments into a page. We have an small web app generating little fragments for different results/gadgets, at various URLs. This is not necessairly on the same domain as the main page.
I was wondering what's the best way of including these into the main page? We also need the ability to skin the HTML fragments using CSS from the main page.
My initial thought was iFrames, however, I'm thinking the performance of this might not be great, and there's restrictions on CSS/JS manipulation of the included fragments.
Are SSI a better idea? Or should we use php includes? or JS? Any other suggestions? The two main considerations are performance of the main page, and the ability to style/manipulate the included fragments.
Cheers,
Victor
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这听起来与 Facebook 平台应用程序的做法类似。一种简单地使用
IFRAME
,另一种从后端获取输出并对其进行转换 -
元素被扩展,JavaScript 被执行,以及诸如按钮之类的东西被剥皮。你可以看看他们的例子。使用 IFRAME 可能会让事情变得复杂。默认情况下,您无法从外部框架修改它们内部的样式,但您可以使用类似 Google Closure 的 net.IframeIo 可以解决这个问题。
我会尝试使用跨域脚本加载小部件。然后,您可以根据需要将小部件的内容添加到页面,例如将其插入到 DOM 中。
This sounds similar to what Facebook Platform applications do. One kind simply uses
IFRAME
s, the other takes output from a backend and transforms it --<fb:whatever>
elements are expanded, JavaScript executed, and things like buttons are skinned. You could look at them for an example.Using
IFRAME
s would probably make things complicated. By default you cannot modify styles inside them from the outer frames, but you could probably use something like Google Closure's net.IframeIo to work around that.I would try loading widgets using cross-domain scripting. Then you can add the widget's content to the page, however you wish, such as inserting it into the DOM.
iFrame 不应该成为性能方面的问题 - 无论是浏览器查询我们的服务器,都不会产生任何影响。但您可能会遇到设计问题。
SSI 和 PHP 或多或少是相同的,但它们都有相同的问题:如果源页面宕机,整个页面的渲染就会延迟。
就性能而言,最好的办法是读取代码片段的缓存 PHP 解决方案,因此不易受到中断影响。
有趣的是,我为此目的编写了一个基于 PHP 的工具,并且我编写该工具的公司已同意将其作为开源发布。不过,至少还需要四个星期,直到我抽出时间来打包它并设置文档。尽管时间有限,如果您对此感兴趣,请告诉我,我会及时通知您最新情况。
iFrames should not be a problem performance-wise - It won't make a difference whether it's the browser doing the querying our your server. You may get design problems though.
SSI and PHP are more or less the same, but they both have the same problem: If the source page is down, rendering of the whole page is delayed.
The best thing performance-wise would be a cached PHP solution that reads the snippet, and is thus less vulnerable towards outages.
Funnily enough, I have written a PHP-based tool for exactly this purpose, and the company I wrote it for has agreed on publishing it as Open Source. It will be at least another four weeks, though, until I will get around to packaging it and setting up the documentation. Should that be of any interest to you despite the timeframne let me know and I will keep you updated.