在 HTML 中创建模板的最佳实践
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16132341/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Best practice to create a template in HTML
提问by Saint Dee
I started creating web pages in plain HTML. I have a header, main content and a footer. The main content is divided into two, left and right content. Everything is the same on all pages except for the right content. Now I have about 15 pages. This is very tiresome if I make some changes on the other static pages (header, footer and left side content) as I have to go through all pages.
我开始用纯 HTML 创建网页。我有一个页眉、主要内容和一个页脚。主要内容分为左右两部分。除了正确的内容外,所有页面上的所有内容都相同。现在我有大约 15 页。如果我对其他静态页面(页眉、页脚和左侧内容)进行一些更改,这将非常令人厌烦,因为我必须浏览所有页面。
What is the best way to create one HTML file for each static area and how am I going to integrate it?
为每个静态区域创建一个 HTML 文件的最佳方法是什么,我将如何集成它?
采纳答案by swshaun
There are a couple of ways to do this.
有几种方法可以做到这一点。
If you are working purely with HTML, then you can use a 'server side include' -- this is a tag which allows you to load a file into the the page (see http://en.wikipedia.org/wiki/Server_Side_Includes). Whoever is hosting your website has to support this.
如果您纯粹使用 HTML,那么您可以使用“服务器端包含”——这是一个允许您将文件加载到页面中的标签(请参阅http://en.wikipedia.org/wiki/Server_Side_Includes)。无论谁托管您的网站都必须支持这一点。
<!--#include virtual="../quote.txt" -->
If youe web host happen to use PHP, you can do this using the include method (see http://php.net/manual/en/function.include.php):
如果您的网络主机碰巧使用 PHP,您可以使用 include 方法执行此操作(请参阅http://php.net/manual/en/function.include.php):
<?php include('path_to_file/file.htm');?>
This will include the file at the point you place the tag. But again, whoever is hosting your site needs to support this.
这将包括您放置标签时的文件。但同样,托管您网站的人需要支持这一点。
There are other methods of doing this, bit these are the two I make use of.
还有其他方法可以做到这一点,这些是我使用的两种方法。
回答by Quentin
There are, essentially, three places you can do this:
基本上,您可以在三个地方执行此操作:
- At build time. Run a script that puts the pages together and outputs static HTML. Then upload the static HTML to the server. ttreeis great for this. You could wrap the script up in a complete build system that combines and minifies your CSS and JS and then uploads it to the server. This is the option that needs the least amount of worrying about what clients and servers support. Update: These days there are plenty of static site buildersthat incorporate this functionality.
- At runtime on the server. This could be a simple SSI, a PHP includeor a full template system (of which there are many, including Template Toolkitand mustache), possibly running inside an MVC framework (such as Catalystor Django). This is the most common approach, it has less time between making a change and putting the result life then doing the templating at build time.
- At runtime on the client. This adds a dependency on JavaScript, so I wouldn't recommend it unless you already had a server side solutionin place.
- 在构建时。运行将页面组合在一起并输出静态 HTML 的脚本。然后将静态 HTML 上传到服务器。ttree非常适合这个。您可以将脚本包装在一个完整的构建系统中,该系统组合并缩小了您的 CSS 和 JS,然后将其上传到服务器。这是最不需要担心客户端和服务器支持什么的选项。更新:现在有很多包含此功能的静态站点构建器。
- 在服务器上运行时。这可能是一个简单的SSI、一个 PHP包含或一个完整的模板系统(其中有很多,包括Template Toolkit和mustache),可能在 MVC 框架(例如Catalyst或Django)中运行。这是最常见的方法,它在进行更改和放置结果生命之间的时间更少,然后在构建时进行模板化。
- 在客户端运行时。这增加了对 JavaScript 的依赖,因此除非您已经有了服务器端解决方案,否则我不会推荐它。