tags:

views:

184

answers:

13

Simple problem I have so far always solved via PHP:

You have a site with header, menu, footer and content field.

Header, menu and footer are usually the same for each page.

  • How do you, without PHP or any other server-side language, have the header, menu and footer data exist only in one file?

So that for example you don't have ten pages (like home.html, products.html, about.html, ..) all having a copy of the static header and menu in their html files. Now if you want to change the header you have to change ten files.

I hope I made my question clear enough, if not please leave a comment :)

+5  A: 

Frames can do this. Can't say much about the quality of the result though.

Ignacio Vazquez-Abrams
Eeew. Better PHP then.
cyggi
Don't even try. There are good reasons why most sites don't use frames anymore.
Techpriester
The other route is to use iframes, or something with JavaScript to insert elements into the DOM, but those are just as nasty.
R. Bemrose
+7  A: 

If you're using Apache, you can use server-side includes. These basically provide include statements within HTML documents.

Matt S
*"[...], without PHP or any other server-side language[...]?"*
cyggi
@cyggi, this is without any server-side language. Apache itself parses the file to include other files.
Matt S
@cyggi This doen't not have anything to do with PHP. It is built into Apache. It doesn't require any 'real' server-side language, so I say it would fit your use perfectly.
Chacha102
A "language" is only something Apache doesn't do itself? And what is a "real language" in that regard? (turing complete?)
VolkerK
You can also do this with IIS, or at least you could years ago, before I switched away from Redmond...
Eli
@Eli: You can still do it with IIS http://stackoverflow.com/questions/1827786/configuration-of-ssi-in-iis-7
R0MANARMY
I have to wonder: how is using mod_includes really different from using mod_php?I suppose maybe mod_includes ships with most apache binaries by default while mod_php doesn't necessarily?
Frank Farmer
-1: While the answer gets around the letter of the question (and even that is arguable... it has variables and control structures, for example), it blatantly ignores the intent.
R. Bemrose
@Bemrose: How so? It involves no server-side programming, as requested. The "intent" is to use no PHP or other server-side scripting, which this does.
Matt S
A: 

There is couple of client side solutions, like various frames or javascript, but all of them either user- or SEO unfriendly.
Why?

Col. Shrapnel
What do you mean with *Why?*?
cyggi
For the record, frames are obsolete as of HTML5.
mattbasta
@cyggi "Why" stands for "Why do you ask"
Col. Shrapnel
+1  A: 

if your host support shtml files, they have limited scriptability.

<!--#include file="included.html" -->

otherwise, for plain html, you're out of luck i'm afraid, unless you have some fancy javascript/ajax that does it.

oedo
Nowadays it's easier to find a host with PHP support than SSI one %)
Col. Shrapnel
hahaha, true dat :D
oedo
A: 

One option that you have is to include the header in references JS file where you have document.write() wrapped around the header. Of course, I do not use this method for my own code since I usually do PHP or VB.

websch01ar
+4  A: 

Could also do it entirely with client side scripting. Just execute some async requests to get static resources and inject them into the page. This is really just a JS version of frames and not the best way to approach the problem, but it'd work.

R0MANARMY
You *could* go skinny dipping in the Arctic Circle, but that doesn't mean it's a good idea ;)
mattbasta
@mattbasta: Pretty much. To be fair the question asked how, and most people have included a disclaimer that it's a bad idea in their answers =).
R0MANARMY
@mattbasta: I also feel obligated to point out that just because it's not a good idea usually doesn't keep people from doing it http://www.youtube.com/watch?v=c1BrcL-ENfI.
R0MANARMY
I've heard of a lot of sites that only have a script include in the actual html file. I do not recommend this but hey it works I guess
Earlz
+1  A: 

Don't know if it's exactly what you mean, but you can do this with SSI (server-side includes) instead of server-side scripting. SSI isn't scripting, it's just a way of including other files without writing any real code.

As for doing this without the server's help altogether, that's a different story. You're better off using the server to do this kind of thing. It feels dirty, you end up with weird files all over, and it's a pain to manage, but templates and includes do a fairly decent job of doing what you're looking to do.

mattbasta
A: 

The most obvious way I can think of that doesn't involve a server language (but does involve server-side processing) is server side includes.

Andy West
A: 

If you had to, and without frames, ajax requests to get the data would be best. Though, as mentioned, would kill seo

Dan Heberden
A: 

If you can use javascript and jQuery, you can dynamically load header and footer using $.load. Not the most clean solution though.

kgiannakakis
A: 

The solution I like best is to, since you know PHP, produce a PHP script that, given a folder of templates, produces a folder of final HTML files for you to upload. Sounds like a decently easy task, actually.

Matchu
+1  A: 

The suggestions of using PHP include or Apache server-side includes are true, these would solve the problem.

But I suggest another option specific to PHP, if you know that every page has the same header and footer:

  • auto_prepend_file

  • auto_append_file

See docs at http://www.php.net/manual/en/ini.core.php

Bill Karwin
+1  A: 

Use JavaScript templating, like that provided by Underscore or Closure. More likely: refuse to work for clients who will not pony up for appropriate web hosting, and are basing that decision solely on cost rather than what is appropriate for the site.

Adam Backstrom