Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Html reuse without code

I am creating some static html pages outside a .net and outside a ruby-on-rails environment.

I created a menu I want to share between several pages, but I'm wondering how this is done using regular html constructs (i.e. without .net's master pages and without rail's layouts)

Is there a way to do this without cutting and pasting?

like image 329
Daniel Avatar asked Nov 01 '09 02:11

Daniel


People also ask

Can we reuse code in HTML?

js solve: any code, especially structural things like a header or footer, can be written once and easily reused throughout a project. Until recently it wasn't possible to use components in vanilla HTML and JavaScript.

How do I reuse header and footer in HTML?

You could place your footer HTML in a separate file, and then use javascript to load the HTML content of that file, and append it to a div in your page. Show activity on this post. Place your code in some html file and then use jquery to include in the file you want.


3 Answers

What web server are you using? It's likely you'll have to enable Server Side Includes in order to use:

  1. Save the HTML for the common elements of your site as separate files. For example, your navigation section might be saved as navigation.html or navigation.ssi.
  2. Use the following SSI tag to include that HTML in each page.

      <!--#include virtual="path to file/include-file.html" -->
    
  3. Use that same code on every page that you want to include the file.

Reference: http://webdesign.about.com/od/ssi/a/aa052002a.htm

like image 166
OMG Ponies Avatar answered Oct 15 '22 09:10

OMG Ponies


To share common HTML snippets between pages, you'll need some sort of server-side "code".
The simplest thing you could do that I know if would be Server Side Includes, "SSI"

see: http://httpd.apache.org/docs/1.3/howto/ssi.html#includingastandardfooter

like image 38
James Maroney Avatar answered Oct 15 '22 08:10

James Maroney


There are basically two options: frames (or iframes) or javascript. Frames come with a whole host of problems and I really don't recommend you go down this route. Have a look at PURE javascript library for clean and simple client-side templating.

like image 31
Ramon Avatar answered Oct 15 '22 07:10

Ramon