Untangling Spaghetti HTML ========================= by Sunil Tanna, EBookCompiler.comWhen I was first learning to write computer programs, one of
most important points that was drummed into me was that it's never a good idea to write great chunks of computer code that are repetitions of earlier code. The reason is simple: when it comes time to update
code, you'll have a tough time making
same change in each copy, and you'll have an even tougher time making sure that each changed copy has been modified in
same way.
Why this digression into programming lore? If you create web sites , there is a good chance that, at least most of
time, you don't think of your HTML files as "computer code". You probably think of them as content, documents, my writing... etc. But
truth is they are. You need only open an HTML file in a plain text editor, and you can see
HTML language.
So
question is, can
same spaghetti problem arise with HTML code? If you've been a webmaster for a while, you probably already know
answer... absolutely. The typical web site contains lot of repeated elements.
For example, you may have
same heading or footer on each page. You might have a search box or banner code on each page. You might have
same menu down
side.
Updating all these multiple copies of
same HTML code in different files quickly gets tiresome. Even if you haven't encountered
update-problem yet, as your projects grow larger and larger, you soon will.
HOW TO AVOID SPAGHETTI?
One answer is
various "include" mechanism that HTML technologies provide:
1. Server Side Includes (SSI)
This technology is implemented on many web sites, and basically allows you to include a common fragment of HTML code (stored in a separate file) into as many HTML pages as you want. The advantage is that instead of updating each file individually, you only need update
common fragment once.
Here's how:
* On most web servers, you simply rename your HTML files (the ones that are doing
including) with a .shtml extension.
* Where you want to include
common fragment of code use a line like:
[!--#include file="fragment.html" --]
IMPORTANT: Because some email programs don't display greater-than and less-than signs properly, I've used square brackets, but when you copy this line make sure to change
brackets to greater-than and less-than signs. If you prefer to see
code online, (and displayed correctly), go to: http://www.suniltanna.com/spaghetti.html
2. ASP Includes
For NT web servers, in .asp pages, you can
exact same include syntax as for SSI.
3. PHP Includes
If your web site supports PHP (a script language that runs on
server), another way to achieve
same thing is to use
PHP include command.
Here's how:
* Rename your HTML files (the ones that are doing
including) with a .php extension.
* Where you want to include
common fragment of code use a line like:
[?php include 'fragment.php' ; ?]
IMPORTANT: Again
same point about
brackets applies. If you prefer to see
code online, (and displayed correctly), go to: http://www.suniltanna.com/spaghetti.html
NO THROUGH ROAD HITHER?
Right, so this all sounds fantastic, problem solved?
Not so fast... While these are powerful techniques they also have drawbacks and limitations:
1. Most web design programs don't know anything about these types of includes. This means you have to work in a text editor on
raw HTML code, manually chopping up your files into fragments, and inserting
include commands into
HTML code. Worse yet, once you've done it, it's quite possible your web design program won't allow you to make any further changes.