HTML question: "including" chunks of code?

Super Macho Man

macrumors 6502a
Original poster
Jul 24, 2006
505
0
Hollywood, CA
Sorry if this is an obvious question. I don't know where to look for an answer or what this might be called. My last experience with HTML was from the HTML 3.0 days. :D

Let's say I have a few static HTML pages on a site. They are all supposed to have the same "look." They all link to the same CSS file, and I've designed them similarly etc. But I am wondering if it is possible to define common "code chunks" and just reference these chunks from each individual page. E.g. so that if I change a chunk, I change every page that uses that chunk. It would sure beat going through 8 different HTML files to adjust some tiny little property in my site-wide footer every time I want to do that.
 

cbetta

macrumors regular
Jul 5, 2006
103
0
use a dynamic language for this, like php, asp, jsp etc. What you do i just include your chuncks where you need them.
 

Nermal

Moderator
Staff member
Dec 7, 2002
18,923
1,393
New Zealand
I don't know of a pure HTML way of doing that, but you could put your chunks into .js files, using this kind of structure:

Code:
document.write('<table><tr><td>My Chunk</td></tr></table>');
document.write('<img src="hello.jpeg" alt="Hi" />');
etc.

Then reference them with:

Code:
<script language="javascript" type="text/javascript" src="mychunk.js" />

Note that this is from memory so there may be a couple of syntax errors :)
 

MrSmith

macrumors 68040
Nov 27, 2003
3,046
14
Javascript would be troublesome as you'd need to put document.write etc. around every line. With PHP you can write one sheet and refer to it from other sheets. For example, write the common code in common.php. Then just write:

<?php
include ('common.php');
?>

in the html page wherever you want the common code.

PHP is great because you can just pop in and out of it on a html page. Like this:

<some html>
<?php
some php code
?>
<back to html>

The server needs to have PHP on it, of course.
 

c-Row

macrumors 65816
Jan 10, 2006
1,193
1
Germany
Keep in mind that there are still some users who turn off JavaScript. So you should either keep important things out of the js. files (like a site navigation - would be bad if that was missing) or provide stripped down alternates within the <noscript> tag which are likely not to change as regularly.

The number of no-JS users gets smaller all the time, but still... ;)
 

ChicoWeb

macrumors 65816
Aug 16, 2004
1,120
0
California
You can use .shtml as well. In case you don't want to use PHP or ASP. I guess it depends on your server. I think .php or .asp looks more profesh, but I guess it depends what you are looking to acomplish.
 

Super Macho Man

macrumors 6502a
Original poster
Jul 24, 2006
505
0
Hollywood, CA
Javascript would be troublesome as you'd need to put document.write etc. around every line. With PHP you can write one sheet and refer to it from other sheets. For example, write the common code in common.php. Then just write:

<?php
include ('common.php');
?>

in the html page wherever you want the common code.

PHP is great because you can just pop in and out of it on a html page. Like this:

<some html>
<?php
some php code
?>
<back to html>

The server needs to have PHP on it, of course.
That is exactly what I was looking for. I tried it and it works beautifully. Thanks for all the help!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.