管理静态 HTML 网站中重复代码的最佳方法是什么

What is the best way to manage duplicate code in static HTML websites(管理静态 HTML 网站中重复代码的最佳方法是什么)

本文介绍了管理静态 HTML 网站中重复代码的最佳方法是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在管理一个包含大量静态 HTML 网站的旧网站,没有服务器端脚本,只有纯 HTML/CSS,最少的 javascript.我发现在不同页面中多次更改同一段代码非常浪费时间.例如,当菜单中的某些内容发生更改时,由于菜单只是每个文档中的静态文本,因此我必须多次进行相同的更改.

I am managing a legacy website with a lot of static HTML websites, no server side scripting, just plain HTML/CSS, minimal javascript. I found it a huge waste of time to change the same piece of code numerous times in different pages. For example, when something in the menu changes, since the menu is just static text in each document, I have to make the same change numerous times.

我想知道将这种开销降到最低的最佳策略是什么,换句话说,对于跨多个静态 HTML 页面管理诸如导航代码之类的事情,您会推荐什么.

I was wondering what the best tactic would be to minimize this overhead, in other words what would you recommend for managing things like navigation code across multiple static HTML pages.

我知道你可以使用:

  1. 服务器端脚本(如 PHP),但没有其他理由在该特定站点使用脚本.
  2. 框架(但这很糟糕,因为它......好吧,框架:))
  3. 服务器端包含(似乎在更改服务器时可能会导致麻烦)
  4. javascript(不利于 SEO)

你会推荐什么?

谢谢.

推荐答案

在所有可能性中,无论是您列出的内容还是我所知道的任何其他内容,我仍然会使用基于 PHP 的简单解决方案来运行.它既简单又干净,几乎不需要您付出任何努力.我对我设计的所有小型网站都这样做.

Out of all the possibilities, both what you listed and anything else I know of, I'd still just run with a simple PHP-based solution. It's easy and clean, requiring next to no effort on your part. I do this with all the small sites I design.

大多数情况下,您会得到相当简单的结构.您写了一整页,然后对于每个后续页面,您只需更改内容所在的中间部分.在这种情况下,只需将内容上方和下方的所有内容保存在 header.php 和 footer.php 文件中,然后放入 在每个内容文件的顶部(与页脚文件类似).完成!

Most often, you end up with fairly trivial structure. You write up a full page, then for each subsequent page you're just changing the bit in the middle where the content lives. In that case, just take everything above and below the content and save it in header.php and footer.php files, then put <?php require_once "header.php"; ?> at the top of each content file (and similarly with the footer file). Done!

这确实有一些小缺点.一方面,您依赖于脚本,但 PHP 是世界上部署最广泛的服务器端语言,所以这不是一个真正的问题.你甚至不在乎它是 PHP4 还是 PHP5,因为你没有做任何花哨的事情.

This does have some minor disadvantages. For one, you're depending on scripting, but PHP is the most widely deployed server-side language in the world, so that's not really an issue. You don't even care if it's PHP4 or PHP5, since you're not doing anything fancy.

对于两个,您在每次页面加载时都运行一个脚本,以便提供本质上是静态文件的内容.这会减慢您的响应速度,并对 CPU 造成不必要的压力.这可能无关紧要(滞后非常小),但如果您发现它很浪费,那么有一些很好的 PHP 框架可以为您提取 PHP 生成的页面并从中生成静态 html.(这也很容易自己做,只需使用输出缓冲做一些工作.)这样你就可以两全其美 - PHP 模板(如果你最终想要更花哨的东西,还可以使用完整的 PHP 语言) 但静态 html 页面可实现最少的 CPU 活动和最快的页面加载时间.

For two, you're running a script on every page load, in order to serve what is essentially a static file. That slows down your responses, and stresses the CPU unnecessarily. This probably doesn't matter much (the lag is very minor), but if you find it wasteful, there are some good PHP frameworks which will take your PHP-generated pages and generate static htmls out of them for you. (This is easy enough to do yourself, too, with just a bit of work using output buffering.) This way you get the best of both worlds - PHP templates (and the full PHP language if you end up wanting something fancier down the line) but static html pages for minimal CPU activity and fastest page load times.

这篇关于管理静态 HTML 网站中重复代码的最佳方法是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:管理静态 HTML 网站中重复代码的最佳方法是什么

基础教程推荐