What is the best way to manage duplicate code in s

2019-01-13 04:39发布

问题:

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.

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.

I know you can use:

  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)

What would you recommend?

Thanks.

回答1:

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.

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!

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.

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.



回答2:

You can use a static site generator. I recommend jekyll.



回答3:

If you are set on maintaining a static site, I would recommend using a static site generator.

One I have used in the past is webgen

From the webgen page:

The page layout is separated from the content: if you change the layout, all pages that use that layout are automatically updated. You can have any number of different layouts and even nested ones.

Write content in a markup language: The content and layout files can be written in a markup language like Markdown, Textile or Haml which lets you concentrate more on what you write.

Automation: webgen can automatically generate, for example, menus and breadcrumb trails for you.

Dynamic content: It is easy to add some dynamic content if there is a need for it.



回答4:

You could preprocess your website with PHP and then just upload the generated static HTML files.



回答5:

It's been a long time since I've used it, but Dreamweaver was a great tool for working with static sites. It had a templating/repeating region mechanism that used comments for this purpose.

Edited to add: A little Googling jogged my memory. Dreamweaver has templates that are similar to ASP.NET master pages. For other content, it uses the metaphors of a Library and Assets. Since this is a static site, you should be able to pick up an older version of Dreamweaver on the cheap that meets your needs.

Edit 2: I have a soft spot for Dreamweaver. If StackOverflow is the anti-Experts Exchange than DW is the anti-FrontPage. Adobe being Adobe, at this point they've probably added enough features to effectively cripple it.



回答6:

There may not be any other reason to use server side scripting, but certainly reducing the amount of written code is a pretty big reason to use it wouldn't you think? It would make maintaining the site much more efficient.



回答7:

In general I'd recommend PHP - its handling for includes is exactly what you need, and it makes anything dynamic much, much easier to manage.

It is also extremely easy to find hosting with PHP installed.



回答8:

As everyone else has said, a static site generator is the way to go.

DocPad is new static site generator, built with Node.js and CoffeeScript it is able to support cutting edge markups like coffeescript, coffeekup, jade and stylus along with the usual markdown and haml support among others.

If you're completely new to the static site generator concept, and would like to know what templating engines and meta-data are, give this article a read.



回答9:

You could use sed to batch-edit files containing the same page elements.



回答10:

What is the big problem with using PHP? In my opinion using an easy PHP include could save you a lot of time instead of editing numerous files. It makes sense.

<?php include('navigation.php'); ?>

Other than that the othe roption is making it on one and then copy and pasting it to the the other pages.



回答11:

I would use PHP whenever my clients would present me with websites of this sort. You can easily put all of the recurring HTML in one file and call it via functions, or put it in separate files and call it via includes/requires/what have you.

Best of all, if whomever you are maintaining the site for, wishes to have some way that they can add content themselves. You already would have enough of the necessary framework in place to make it very simple for them.



回答12:

I've used Webby for this in the past and was very satisfied with how easy it made things and reduced duplication.



回答13:

I'd say an inline frame can be just fine for something like a menu that appears on every page and needs to be updated on every single one. Just remove the frameborder, size it properly, and it should be good.



回答14:

I think a reasonable compromise between ease and speed would encompass Server-Side-Includes first, then PHP later.

As for PHP, I'd suggest you wrap the content by using auto_append_file and auto_prepend_file directives for the Apache2 Module.



回答15:

<?php include('header.php') ?>

//Your contents are placed here

<?php include('sidebar.php') ?>
<?php include('footer.php') ?>


回答16:

you can include static webpages without using php but instead bu using javascript.

here is an example :

<!DOCTYPE html>
<html>
<script src="https://www.w3schools.com/lib/w3.js"></script>

<body>

<div w3-include-html="h1.html"></div> 
<div w3-include-html="content.html"></div> 

<script>
w3.includeHTML();
</script>

</body>
</html>


回答17:

I would put all crawlable content into the HTML, then generate the repetitive content using javascript. So something like this:

<!doctype html>
<html>
    <head>
        <title>My website</title>
    </head>
    <body>
       Main content goes here.
       <!-- This script should generate the extra elements, 
            the navigation bar and stuff around the main div. -->
       <script src="enhance.js"></script>
    </body>
</html>

So all you need to do is setting the title and writing the main content in each page. The rest (which is probably not interesting to crawlers anyway) is generated by JS.