Requirements:
1. Access to a server with ASP installed (if the server is running IIS, then ASP should be supported)
2. A text editor to create the files
3. A working knowledge of HTML
What's this? HK is writing an ASP tutorial?!
I do actually write ASP scripts on occasion, when the job demands - and it is never a good idea to forget that there are more ways to skin a cat.
In all honesty, there is not much different functionally between the two languages - it is mostly cosmetic, in the form of the syntax that two use.
Fed up of having to make a change to multiple pages because you added a new link to your navigation bar or need to update your disclaimer at the bottom of the page?
Well, with a simple bit of ASP, you can overcome this
For this tutorial, I will only actually create one page as an example, but the principle will work for two, twenty, a hundred, or more, pages.
This tutorial will use vBScript for the ASP sections - it is possible to use JavaScript or even PHPScript, if the relevant software is installed on the server.
The Files
Two files are needed
index.asp and
page.asp. For tidiness, I tend to put the
page.asp into a sub-folder named
includes or similar.
Copies of the two completed files are attached to this tutorial.
The Template
The contents of this file will be the functions to draw the template of the page this will be the common parts of the page, including the HTML skeleton and things like the navigation bar.
For this tutorial, we will use the HTML skeleton from the
HTML 101 tutorials.
Code:
sub page_Header(title)
' creates the common page header HTML
' title = page title in browser
%>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title><% Response.Write(title) %></title>
<%
' do not close the <head> element - allows individualisation
end sub
The variable
title allows us to modify the page title displayed in the browser making it easier for users to find the page in a history listing. It is also a good idea for search engines.
Notice how the <head> section is not completed in this function this is so that an individual page can have extra information placed in this section before we close it. While we would like to have as much as possible placed in a common function like this, sometimes we do need individualisation of a page.
The next function closes the <head> element, and starts the <body>:
Code:
sub page_Bridge(heading, subheading)
' closes the <head> element and starts the <body>
%>
</head>
<body>
<h1><% Response.Write(heading) %></h1>
<h2><% Response.Write(subheading) %></h2>
<hr />
<%
end sub
The use of
heading and
subheading allows us to create individual headings on the page.
Code:
sub page_Navigation()
' creates the navigation links
%>
<div class='standard' id='div_nav'>
<a href='index.asp'>Home</a> ||
<a href='about.asp'>About</a> ||
<a href='links.asp'>Links</a>
</div>
<%
end sub
The rest is all pretty much self-explanatory (and is available in the attached file)
The Page
Once you have your template in the
page.asp file, you now need to create the actual page (in this case
index.asp) for display.
Code:
<%
' index.asp
' Created by: Stuart Jones
' Created on: 10th October 2005
' This file is freely distributed as part of a tutorial
%>
<!-- #include file="includes/page.asp" -->
<%
' this means all functions in page.asp are now available in index.asp
page_Header "Example :: Home"
' if you want to, you could put a <script> or a <style> which is required
' only by this page
%>
<script src='scripts/welcome.js' type='text/javascript'></script>
<%
page_Bridge "Example", "Home"
' You could insert more individualised HTML here..
page_Navigation
' ...and here too
page_MainStart
' this is the actual content section - any valid HTML can be entered here...
%>
<p>
Welcome to the ASP/HTML tutorial: "My First ASP"
</p>
<p>
I hope you find it useful.
</p>
<%
' ...and now we close it all
page_MainFinish
page_Disclaimer
page_Footer
' ----------------------------------------------------------------------------
' END OF FILE
%>
The reason why sections are broken up into the following functions:
Code:
page_Header "Example :: Home"
page_Bridge "Example", "Home"
page_Navigation
page_MainStart
page_MainFinish
page_Disclaimer
page_Footer
is that this offers a lot of flexibility in the template, with very little effort in future pages.
I hope that this is useful to anyone starting out in ASP look out for more tutorials at HTML Forums