Forum Moderators: open

Message Too Old, No Replies

How to write a only single copy of repetitive code

To avoid rewriting "menu" html for every page

         

kiril

2:35 am on Aug 12, 2003 (gmt 0)

10+ Year Member



If I have html code, e.g. for the internal menu links that will appear identically on nearly every page of my site, how can I put that code into a single separate file and then have it read in by the code of each page that needs it? The point of course is to avoid having to type (or later modify) the same thing on every page.

Thanks.

msr986

2:43 am on Aug 12, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If your host allows, use server side includes (SSI). This allows you to put all the common code into one file. Then, as the page is retrieved, the server inserts the external file.

There is a lot of info on these boards about SSI's. Try using the site search function from the link at the top of this page.

Bowdii

2:56 am on Aug 12, 2003 (gmt 0)

10+ Year Member



Yup, SSI's work great! Another way would be to use templates. Dreamweaver does a great job with this. create what you want to be on every page. Save as template. Then make your individual pages -> apply template.

Check out dreamweavers help for more details.

Good luck:)

kiril

3:19 am on Aug 12, 2003 (gmt 0)

10+ Year Member



Thanks guys. I searched around WW for SSI and found that I should be putting something like this:

<!--# include virtual="menu.html" -->

in the html of the various pages of my site in place of the code for my menu (i.e. anchor elements, etc), and then the file menu.html will contain that code instead.

I tried this with two html files (e.g. index.html and menu.html) on my desk computer, but it seems to do nothing. Are things more complicated, or does the fact that it is a "server side" include mean it will only work on an appropriately configured server that hosts the site?

Thanks

msr986

3:21 am on Aug 12, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> does the fact that it is a "server side" include mean it will only work on an appropriately configured server that hosts the site

Yup, you got it. Try this on a file hosted on your website.

<added>You may need to put this in your .htacess file for it to work:

Options Includes

</added>

WibbleWobble

8:56 am on Aug 12, 2003 (gmt 0)

10+ Year Member



You may also have to either rename the extension .shtml, or add the options to parse html files for SSIs.

here's my .htaccess for parsing stuff.

AddType text/html .htm .html .shtm .shtml .sht
AddHandler server-parsed .htm .html .shtml .sht .shtm

hartlandcat

9:27 am on Aug 12, 2003 (gmt 0)

10+ Year Member



Unless your webhost is different, you will need to name the pages that you use SSI on as .shtml -- I have known people name them .shtm -- don't do this, it won't work. Well, it will work, but it will only work in Internet Explorer, which is obviously not what you want.

topr8

9:46 am on Aug 12, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



don't do this, it won't work. Well, it will work, but it will only work in Internet Explorer

cough, cough?

the include is included by the server it has nothing to do with the client browser, or have i misunderstood your point?

hartlandcat

2:48 pm on Aug 12, 2003 (gmt 0)

10+ Year Member



You have missunderstood my point. Someone I know had used SSI on their website, and they had named the page on which it was to be included with a .shtm -- it worked in IE, but not Netscape. For HTML pages to display as HTML in Netscape (and most other browsers), they have to have either a .html, .htm or .shtml extension. In Internet Explorer, you can name your pages with a .txt and they will still show up with the layout you wrote.

Visit Thailand

2:57 pm on Aug 12, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you are new to this then really think hard about the future of your pages before you finalise everything.

SSI is very powerful and an amazing tool.

You can even ad SSI's that lead to blank .htm files so you could add ads or whatever at a later date.

I would recommend really thinking about where you want the site to go and what you might want to do with it in the future.

Adding some extra code at the beginning is easy changing thousands of pages later is a lot more work.

drbrain

4:25 pm on Aug 12, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



hartlandcat: That is because the server was misconfigured, and IE was doing the wrong thing and guessing the mime type. IE is in the wrong here. SSI has nothing to do with the client.