問題描述
我正在管理一個包含大量靜態 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.
我知道你可以使用:
- 服務器端腳本(如 PHP),但沒有其他理由在該特定站點使用腳本.
- 框架(但這很糟糕,因為它......好吧,框架:))
- 服務器端包含(似乎在更改服務器時可能會導致麻煩)
- 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 網站中重復代碼的最佳方法是什么的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!