To read the article online, visit http://www.4GuysFromRolla.com/webtech/tips/t030200-1.shtml

User Tips: A Neat Use for Include Files Encompassing both HTML content and ASP Code


By Jeremy J.

I've got a suggestion for a tip I've picked up when making ASP/SQL pages.

I often find my self using a similar design over a large number of pages, and I want to have a header and a footer. I know this could be set up with two lines:

<!--#include file="header.inc"--> and <!--#include file="footer.inc"-->

but for me that means editing more files, and doesn't allow easily to see both files at once, for example if I made a large change to the site design. Also, on some pages I want to include a small form that updates something in a database using SQL, and depending on the results of that, possibly forward the user to another page. For example - I have a logon page for an admin area called logon.asp. The user enters their username on the page and the actual checking of the user is done on that page:

Logon.asp
<%
If request("action") = "logon"
  'check user
  'if valid response.redirect("main.asp")
End If
%>
<!--#include file="header.inc"-->
<form action="<%=scriptname%>?action=logon" method="post">
<input type="text" name="user"><input type="submit"></form>
<!--#include file="footer.inc"-->

However, there can be some problems doing things like this - if the checking part needs to look something up on a database, then I need to specifically set up the database connection string beforehand - if it was in the header, then I'd have to do the checking after the header was shown, and then I couldn't use the forwarding method. If this is the same string as I need for several pages, and it may change, then I need to include a third file called something like database.inc and include that at the top of every page. Help! - 3 include files.

However, I discovered a nifty way of only using one include file, and it certainly solves all my problems. The file is called includes.asp and looks like this:

<!-- Include this information on each page -->
<%
'Set up Connection String
Scriptname = Request.ServerVariables("SCRIPT_NAME")
%>
<!-- End of general information -->

<% Sub Header(PageName) %>
<html>
<head><title><%=PageName%></title></head>
<body>
.. Do header stuff
<% End Sub %>

<% Sub Footer %>
.. Do footer stuff
</html>
<% End Sub %>

Now, creating new database-enabled pages is very simple - I simply write them like this:

<!--#include file="includes.asp"-->
<% call header("New Page") %>
Page Content goes here
<% call footer %>

Simple, eh? Also - if I wanted to put any code at the top of the page, I can put it between the <!--#include line and the <% Call header line. Note, I also used a variable to change the title of the page - other variables could be used for similar purposes.

Now, if I need to change and of the information on pages, I simply need to alter the includes.asp file - where everything is in one place.

Great site guys - keep up the good work! I'll let you know if I think of any other useful things I've learnt in ASP/SQL, mostly from working on the site at http://www.planetquake.com/airquake2/.

Jeremy

Happy Programming!

Return to user tips...


Article Information
Article Title: User Tips: A Neat Use for Include Files Encompassing both HTML content and ASP Code
Article Author: Jeremy J.
Article URL: http://www.4GuysFromRolla.com/webtech/tips/t030200-1.shtml


Copyright 2017 QuinStreet Inc. All Rights Reserved.
Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers