框架可以用什么代替?
假设我有一个包含 50 个网页的网站。每个页面都包含
- 标题
- 导航
- 左侧内容
- 右侧内容
- 页脚
不使用
- 框架
- 服务器端编程
- 第三方工具和软件
- 框架
我需要将这 5 个部分中的每一个的代码(HTML 标记)放入这 50 个页面中的每一个中? “一次编码,一次又一次使用”这个术语怎么了?另外,如果我想改变任何东西,我需要改变 50 个地方。这只是多余的。我可以这样使用吗?
<object type="text/html" height="100%" width="100%" data="header.html"></object>
请原谅我的愚蠢问题。学习硬道理!
Lets say I have a web site with 50 web pages. Each page consists of
- A Header
- Navigation
- Left Side Content
- Right Side Content
- Footer
Without using
- Frames
- Server side programming
- Third part tools and softwares
- Frameworks
I would need to put the code (HTML Markup) for each of these 5 sections in each of those 50 pages? What happened to the term "Code Once, Use Again & Again"? Also If I want to change any thing, I would need to change it at 50 places. This is just reduntant. Can I use as such.
<object type="text/html" height="100%" width="100%" data="header.html"></object>
Excuse my stupid questions. Learning the hardway!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
是的
您拒绝了所有在 HTML 中实现这一点的技术(好吧,除非您从头开始编写自己的模板引擎(没有第三方工具)并在构建时运行它而不是按需运行(没有服务器端代码),或者使用 JavaScript (这太疯狂了))。
这实际上是一个
,只是功能较少且浏览器支持较弱。
Yes
You rejected all the techniques that make that possible in HTML (well, unless you count writing your own template engine from scratch (no third party tools) and running it at build time instead of on demand (no server side code), or using JavaScript (which is insane)).
That is effectively an
<iframe>
, just with fewer features and weaker browser support.您描述了服务器端语言的确切用例。
让我向您解释一下您的方法存在的问题。如果您以这种方式创建您的网站,并通过
object
或iframe
包含index.html
和其他 5 个部分,这将意味着向您的服务器发送 6 个请求(这当然不是一个好的做法)。事情会发生这样:
index.html
header.html
和其他四个从 ,您可以只请求
index.php
例如(如果您使用 PHP),让 PHP 构造 ONE HTML 资源(可能包括其他 PHP 文件,例如header.php)。 php
)并将其发送到浏览器,这会很高兴地显示它。您不必打破学习顺序,只需将一些小部分用于此特定目的(根本只使用一个命令)。保持灵活性,你会发现它在这个行业非常有用:)。
举个例子:
index.php
在你的 PHP 文件中,你只能有简单的 HTML,只是文件扩展名会有所不同。要编写 PHP 命令,您可以使用
You describe the exact use case for a server-side language.
Let me explain you the problem with your approach. If you do your site this way, having an
index.html
and those other 5 parts you include through eitherobject
oriframe
, this will mean 6 requests to your server (which is certainly not a good practice).Things will happen like this:
index.html
header.html
and four othersInstead, you could just request
index.php
for example (if you use PHP), let PHP construct ONE HTML resource (possibly including other PHP files, likeheader.php
) and send it to the browser, which will happily display it.You don't have to break your learning order, only use some small parts for this specific purpose (using only one command at all). Be flexible, you will find it very useful in this business :).
To give you an example:
index.php
In your PHP files, you can have only simple HTML, only the file extension will be different. To write PHP commands you can use
<?php whatever ?>
您还可以通过组合不同的部分来实现这一目标,这意味着您将标题写入一个文件,导航到另一个文件,将内容写入第三个文件,然后将它们组合到实际的网页中。 (基本上是制作自己的模板引擎)。例如(在 UNIX 中)
不是理想的解决方案,但这样您只需要更改一次。但对于任何真实的事情,已经建议的服务器端脚本通常是最佳选择。
You could also achieve the goal by just combining different parts, meaning that you write your header in one file, navigation to another and content to third file and then combine these to actual webpages. (basically making your own template engine). Such as (in UNIX)
Not the ideal solution but this way you only need change once. But for anything real, already suggested server-side scripting is best option usually.
如果您不想使用服务器端编程(这是它为您而构建的),您唯一的选择是 AJAX 调用来在运行时填充公共元素。
然而,这不会提供非常好的用户体验,因为页面在似乎完成加载后会慢慢加载。
您确实应该为此使用服务器端编程。
If you don't want to use server side programming (which is what it was built for mind you), your only choice is an AJAX call to fill in the common elements at run time.
This however will not provide a very good user experience, as the page slowly loads itself AFTER it seemed to finish loading.
You really should use server side programming for this.
一种常见的解决方案是服务器端包含 (SSI),它允许您在多个页面中包含一个 html 标记块(除其他外)。如果您使用 PHP,请务必考虑 PHP 包含: http://www.w3schools.com/php /php_includes.asp。
One common solution is Server Side Includes (SSI), which allows you to include one block of html markup in multiple pages (among other things). If you're using PHP, definitely consider PHP includes: http://www.w3schools.com/php/php_includes.asp.