Server status script Writing your first XHTML or HTML Webpage
Jun 30

See - save you effort, time and enable you to create standrds driven pages with ease. this is a bit long but it is only an overview so please read it if you are not already using includes to create your pages

Using includes to create a web page:
It is my opinion that any webmaster worth a biscuit should be using inclides in their page design. As such, for those who don’t, I have written a short tutorial in what they are and how to use them. I hope someone finds it useful.

What is an include?:
An include statement is named after exactly what it is! An instruction to the web server to include one file within another. For example, if I write a webpage have it formatted as follows:

HTML Code:

Hi
My webpage

I would like to add “this is” on the empty line but I don’t want to type “this is” on every page I want it to appear (start thinking bigger and look at headers, footers, navigation areas etc)

I ‘COULD’ type “this is” on every page I wanted it to appear, but that seems like a lot of effort. I just want to type it once because I am a typical web developer and I want to find the path that creates the least amount of work. Keep reading and you will see why.
I will use php in this example but I will explain the ASP version later on.

So, I have my page formatted as above, but this time I create a new page that simply contains the words “this is” and I save it as include_thisis.php (the filename is not important) . Then I go back to my original page and change it’s source to read:

Code:

Hi

PHP Code:

<?php include(’include_thisis.php’); ?>

My webpage
So, when anyone views my page now, they see the text from my main page and then where I have included my “this is” file, they see the contents of that file.
My page now appears as :
Hi,
This is
My webpage

I can now include that file anywhere that I want “this is” to appear. Whilst that is, in itself a useful tool for any text, image or ANY content whatsoever that you wish to repeat throughout your site, it’s usefulness does not stop there. If I decide that my pages need a change, I don’t want them all to say “this is” anymore, I want them all to say “that was” instead. Laziness kicks in again (actually, it’s good web practice, but that’s a bonus) and I don’t want to go through all of my pages finding and replacing. Lucky me, I used includes. I go back to my include_thisis.php file and change the text from “this is” to “that was” ALL of my pages that hade the include statement in, now say “that was”:
Hi,
That was
My website

Now, if you have a navigational bar on your site and you add a new page, do you really want to go through adding that link to every single page, then, what if you spell it wrong on one of 30 pages, would you know which? FAR better to have it wrong on all of them and very easy to spot and then fix (by changing your one include file).

ASP/PHP:
Whatever your server side language, there is a similar piece of code that enables you to use includes.
In PHP :

PHP Code:

<?php include(’filename.php’); ?>

In ASP:

PHP Code:

<!–#include virtual=”filename.asp”–>

This is a VERY high level simplistic explanation I have written as an introduction to includes. If you would like some examples I can quickly put some together for you, or feel free to ask me.
It is important to understand that these are not FRAMES which should be avoided at all times by all but the most inexperienced web developer as they can create far more problems than they solve. To your visitors all they see is one page (and it looks like you did a hell of a lot more work than you really did).
Hope that starts you off

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • bodytext
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • Technorati
  • BlinkList
  • Live
  • Reddit
  • Slashdot
  • StumbleUpon

Technorati Tags: , , , , , , , , , , , , , ,

One Response to “Using includes to reduce your workload”

  1. Using strings and includes to help development and SEO | Hungerford Web Design Blog WordPress 2.2.1 Says:

    [...] We talked about using php includes to reduce the repetition of code and standardise features here: [...]

Leave a Reply