tags:

views:

291

answers:

5

I'm sure this topic comes up all the time, but I can't seem to fine a concise answer. I've got a vertical menu bar that I want to reuse in our web pages (>20) for the site.
The Menu bar is coded in html (UL, LI, A), uses Div tags and CSS to achieve the desired effects.

We want to find the simplest approach to reusing the menu bar in all web pages while being able to maintain and scale as needed, so we don't have to modify all pages every time we add a page. We'd rather avoid a coding approach if possible, meaning we could live with just one master file that we edit as needed. Sine we're using CSS and DIV's I don't hink frames are the answer. Any thoughts?

A: 

If you would use PHP, all you have to do is use the include command, no coding beyond this one command.
Also, check out server side includes

Itay Moav
+1  A: 

In order to do this, you'll have to use some server side technology. For instance you could...

  • include them in php

  • put them in the master page in .net

  • put this in a partial or a layout page in rails

Some reading:

http://us.php.net/manual/en/function.include.php

http://msdn.microsoft.com/en-us/library/wtxbf3hh.aspx

Another solution would be to create all this using Javascript, but please don't do it like that :)

html:

<script type="text/javascript" src="hack.js"></script>
<div id="mymenu">
</div>

hack.js:

function createMenu(){
  $("#mymenu").html("all the html of your menu");
}
marcgg
i'm wondering what's the disadvantages using the javascript hack over server-side technology.
tomato
+3  A: 

Server side includes are the way to go if you don't want to use a programming language.

They take this form:

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

and will be inserted in the page wherever you put that tag in your HTML. It requires server side parsing, so your web server must have server side includes enabled. You can try it out, and if it doesn't work, contact your server host to see if you can get them enabled. If it's Apache, there's a method of enabling them via .htaccess files as well.

zombat
+1 for a framework agnostic, simple html based solution
desigeek
A: 

So far one of the best solutions I have found is to model the menus after the Son of Suckerfish XHTML/CSS solution that is pretty well documented on the internet now combined with some logic on the server to render the unordered list. By using unordered lists you have a couple different options on how to output the results, but as long as the menu has some basic hierarchy you can generate it. Then for the actual page all you need to do is include a reference to the menu generating function.

Rob
A: 

I've done this two separate ways - one using server side (PHP) and one using Javascript includes (for demos that need to be able to run without any internet connection or server capabilities).

For PHP includes your pages will have to end with .php rather than .htm or .html, and these are very ideal to replace your header, footer, navigation, etc. Anything that is repeated on multiple pages.

Basically you would create your normal code then copy and paste the code you want to break out - in this example, your navigation - and save it in another file called (for example) inc_navigation.htm (this page can be called .htm).

Then in your actual pages you'd use the following code:

<?php include('inc_navigation.htm') ?>

That would insert your navigation at that point, if you had a change to make you'd make it to the .htm file and it would propagate to any page with that included.

For javascript includes you will have to include the following line at the top of every document where you want to include your navigation:

<script type="text/javascript" src="includes.js"></script>

Then you'll create a document called includes.js.

At the top of this document you'll declare your navigation variable:

var navigation  = new Array();  // This is for the navigation.

Then a little ways down in that same document you need to actually outline your navigation code (the line numbers in the square brackets are crucial - keep them in order and start with 0 - you cannot have line breaks in this code so every line of code has to be a new line):

// ==================== Navigation ==================== //
navigation[0]   = '<div id="tab_navigation">';
navigation[1]   = '<ul id="dropline">';
navigation[2]   = '<li><a href="index.htm"><b>Home</b></a></li>';
navigation[3]   = '<li><a href="about_us.htm"><b>About Us</b></a></li>';
navigation[4]   = '</ul>';
navigation[5]   = '</div><!-- Close TAB NAVIGATION -->';

Then a little ways after that you'll actually insert the javascript that will put that code into your page (it doesn't actually put it there but rather makes it accessible in the page without actually altering the code of the .htm page - so if you view source you'll see the reference to the code not the code itself).

function show(i)
 {
  for (x in i)
  {
   document.write(i[x]+'\n')
  }
 }

Finally - in your .htm document, say for your index.htm page, you'll replace your navigation code (that you put in the above block called navigation) with this:

<script type="text/javascript">show(navigation);</script>

Where that name after SHOW and in the parenthesis is the name of your variable (declared earlier).

I have sites showing both methods in use if you'd like to see them just send me a message.

Elle