views:

61

answers:

3

Hi everyone,

Very basic question: I am coding a web app that has a handful of pages. These pages have the usual shared elements: eg, the site's header/masthead and a side-bar are present on all pages. The HTML is static (not dynamically generated, its "ajaxy-ness" is done client-side).

What is the best way of importing/"including" those common elements into my pages? The solution I am using is to have the HTML files contain empty place-holders

<div id="header"></div>
<div id="leftSideBar"></div>
(...)

and then do in jquery's $(document).ready():

$.get("header.html", function(html) { $("#header").html(html); });
// ....

Is this the best way to do this? I'm new to web development. : )

I guess I could also dig up a "macro-like" code-generation tool that I would run on the HTML files to replace, eg, "#header" with the contents of header.html. That way loading a page would require a single request for a single HTML file, which sounds better.

What is the smart way to achieve this? I am sure this problem has been solved a thousand times.

EDIT: The server-side is coded in Python+cherrypy. (I am assuming it is reasonable to try to keep away from dynamically generating HTML when doing "web 2.0-ish" web apps. Please correct me if I am wrong. As I said, I am very new to this environment.)

Thank you for your insights,

lara

+5  A: 

If you want to include files, please consider using some backend language such as PHP or ASP. Javascript is not really meant to do this even if this would work.

<?php include 'other_file.php'; ?>

Using javascript to do this will lead, I think, to a poor SEO and the loading of the page might look weird for the end user. If you really don't want to use a backend language, some IDE have a way to handle templates, you could look into that.

Concerning frameworks, most of them have a way to handle templates. ASP.NET has the master page system, Ruby on Rails has layouts.

Here's an example using Rails :

<html>
...
<div id="content"> <%= yield %> </div>
...
</html>

Here all the content of a subpage will go into the "yield". Here's a link to learn more about that.

Some frameworks can handle multiple place holders.

marcgg
considering the accessibility, using javascript to pull content is just hell of a wrong approach.
dusoft
Marc, I'm not married to the idea of including files. I am just wondering how people who don't generate the html dynamically avoid duplicating common page elements across all pages of their sites. thanks for any help.
laramichaels
@lara: I'd say that these people either use a template (cf my edit on dreamweaver) or simply duplicate the code.
marcgg
thanks for the edit, that made it clearer!
laramichaels
If you're not using a server side scripting language, then some form of Server Side Include functionality or simply generating the individual pages from templates using scripting languages are certainly options to consider.
Adam Luchjenbroers
+1  A: 

To some extent, it depends on what you're using on the server side to render the pages. If your using server side scripts to generate the page you should be able to use a web framework (eg. Django or RubyOnRails) or even just a basic templating engine such as Genshi. Basic include functionality may even be built into the language you're using (ie. PHP)

If it's just static HTML you may want to look into setting up some form of server side includes such as Apache SSI or NGINX SSI. You'll need to pick the one that works with whichever server you're using, and you'll need enough access to install and configure the plugin or module.

Alternatively, you might want to look at using a script to generate your pages (edit, generate and deploy). A simple approach using cat / sed / awk / make (additional useful reference - Sed & Awk) may be all you need, or you might want to use a templating engine and a language such as Python or Perl.

Adam Luchjenbroers
many thanks! The web app is coded in python, but I am (ill-advisedly?) trying to keep all HTML static. I didn't know NGIX could do that for me, that should solve it. : ) -l
laramichaels
A: 

I'd have the includes handled server-side, and this will mean fewer requests from the client, and may also have other benefits (easier to debug js, etc).

Having the server process includes really isn't going to put a major strain on it.

MarkR