什么是在静态HTML网站中管理重复代码的最佳方式 [英] What is the best way to manage duplicate code in static HTML websites

查看:148
本文介绍了什么是在静态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. 服务器端脚本,但是没有其他原因在特定网站使用脚本。

  2. 框架(但这是坏的,因为它的..好,框架:))

  3. 服务器端包含(更改服务器时可能会导致问题)

  4. javascript(不适用于SEO)

  1. server side scripting (like PHP), but there is no other reason to use scripting at that particular site.
  2. frames (but that's bad because its.. well, frames :))
  3. server side includes (that seems like it could lead to trouble when changing the server)
  4. javascript (bad for 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文件,然后把<??php require_onceheader.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网站中管理重复代码的最佳方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆