如何创建 DRY HTML?
我有一个小型静态网站,该网站的每个页面都有一个菜单和页脚。
确保菜单和页脚的更改只需要在一个地方完成并使我能够轻松更新包含它们的所有页面的最佳方法是什么。
我正在寻找某种简单的模板系统,使我能够将文件组合在一起。
我已经研究了一些 ruby .erb 文件,但它们对于我想要实现的目标来说似乎太复杂了,因为我必须安装 Rails 并使我的网络服务器能够使用它。
I have a small static website and every page of this site has a menu and a footer.
What is the best way to make sure changes in the menu and the footer only need to be done in one place and enable me to easily update all my pages which consist of them.
I am looking for some kind of simple template system that enables me to combine files together.
I have looked a bit into ruby .erb files but they seem too complicated for what I want to achieve as I would have to install rails and enable my webserver to use that.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
对于一个简单的网站,服务器端包含没有什么问题。只需为您的菜单和页脚创建 HTML 片段(它们甚至不必是完全形成的 HTML)。然后在每个页面上的
适当位置添加适当的语句。由于您使用的是 Debian 服务器,我很确定 Apache 已经默认启用了此功能。
这可能看起来已经过时了,但我妻子在一家为小型网站做大量维护工作的公司工作,他们仍然采用这种方法,而且效果很好。
如果您的网站超过 10 个页面,那么我建议您研究一下一些模板系统,这样就无需记住在您创建的每个新页面上添加 SSI。
For a simple site, there's nothing wrong with doing server side includes. Simply create the HTML snippets (they don't even have to be fully formed HTML) for your menu and footer. Then on each page, add the appropriate
statement in the proper location. Since you're on a Debian server, I'm pretty sure Apache wil already have this enabled by default.
It may seem antiquated, but my wife works for a company that does a lot of maintenance for small websites and they still take this approach and it works just fine.
If your site goes above 10 pages, then I'd say look into some of the templating systems, just to alleviate the need to remember to add your SSI on each new page you create.
您可以查看一些Web 模板系统,并根据您熟悉的语言/平台做出决定和
you could have a look at some Web Templating Systems and decide based on the language/platform you are familiar with
我使用 Octopress。它是一个建立在 Jekyll 之上的静态站点生成器,它使用 markdown 进行内容标记,并使用特定的模板语言来构建页面。因此,如果您只需要一个只有几个页面的网站,您应该尝试 jekyll。
它要求您的系统具有 ruby,因为所有站点生成都是在客户端完成的,然后通过 rsync 部署站点。
I use Octopress. It's a static site generator built on top of Jekyll which uses markdown for content markup and specific template language for constructing pages. So if you only need a site with a few pages you should try jekyll.
It requires for your system to have ruby since all site generation is done on client side and afterwards the site is deployed via rsync.
尝试在互联网上搜索
静态站点生成器
。它提供了多种各种语言的解决方案:Python、Ruby、PHP、Haskell、Sh、Bash……Try searching the internet for
static site generator
. It gives dozen of solutions in all sort of languages: Python, Ruby, PHP, Haskell, Sh, Bash…您需要在服务器端组合这些吗?
对于一个小型静态站点,我只是创建了一个小的本地脚本(我使用了 PowerShell,但请随意使用您想要或可以使用的任何内容),该脚本从代表模板的本地源文件进行部署。虽然在模板方面可能不如成熟的模板引擎那么灵活,但它很简单、快速并且可以在相当长的一段时间内运行良好。此外,它在本地运行,除了服务器端的简单 Web 服务器之外不需要任何东西,从而减少了潜在的漏洞。
Do you need to combine those on the server side?
For a small static site I simply created a little local script (I used PowerShell, but feel free to use whatever you want or have at your disposal) that does deployment from the local source files which represent the templates. While maybe not as flexible on the template side as full-blown templating engines it's easy, fast and works well for quite a while. Also it runs locally and doesn't need anything except a simple web server on the server side, cutting down on potential vulnerabilities.
我使用过 WML (“网站元语言”;注意没有与移动和 WAP 相关的WML!)多年来在 Debian 上保持一致我的 ISP 静态页面托管上页面的模板化页眉/侧边栏/页脚样板。
I've used WML ("Website Meta Language"; NB nothing to do with the WML associated with mobile and WAP!) on Debian for years to maintain consistent templated header/sidebar/footer boilerplate for pages on my ISP's static page hosting.