In ”Add power by using objects within ASP pages,” I demonstrated how you can use objects in your ASP code. Many objects are built into ASP, and others are available to aid in your coding. But no matter how many objects already exist, you’ll always need to develop your own objects to fulfill the needs of each individual project. In this article, I’ll show you a framework for your ASP pages and explain how to develop your own objects and use them in your ASP pages.
Start with the framework
The first thing we need to do is improve the structure of the code from the previous article. I’m going to take the sample musicList.asp file and make it part of a template that simplifies the process of assembling pages. The template consists of only three files, although it can support the addition of more files in order to expand the common elements, such as a standard header and navigation. The pages for the framework are pageDeclare.asp, pagePrep.asp, and pageClose.asp. I have placed these pages in a directory created off the root of the Web directory so it is easy to find them.
Now, before we make a page that uses the framework, we are going to have to learn about includes. The ability to include files improves the modularity of your code. In ASP, the <!— #include..—> statement is the key to placing the contents of one file into another. Be aware of a few things when using includes.
First, the inclusion occurs before ASP processes the file, so you can’t dynamically include a different file based upon programming logic. You could include a number of files separated by if orselect case statements, but that isn’t really the same thing.
Second, you can nest includes so that an included file includes another file as long as an included file does not include a file that originally included the current file (whew!). Obviously, recursion in includes would not be a good idea.
Listing A shows a template file that can be copied in order to create other files in the project.
This page starts with an include statement that uses the virtual keyword to specify that the system should start with the current Web root when trying to locate the file. You can also use the file keyword instead of virtual but then the system will look for the file relative to the current directory of the executing script file. Using the file keyword generally isn’t a good idea since it complicates finding the file and makes it almost impossible to use the same template for all files in your project. Notice the first file included in the pageDeclare.asp file. This file sets up the page and declares any page level variables that might be needed. I have a few that are basic to just about any page. The pageDeclare.asp file appears in Listing B.
The pageDeclare.asp file indicates that we are using VBScript and sets option explicit to ensure that all variables are declared before they are used. The page-level variables are then declared and set to defaults. I set the variables to defaults so that each page using the template is not responsible for setting values to these variables. This allows you to add additional variables to the template without revisiting pages.
Notice that there’s an include statement here for the dbAccess.asp file. This file contains our VBScript object (which is the subject of the next article). Right now, just notice that the variable dbConnect is set to a new instance of the object called DBConnection. This means that every page will have access to an object variable called dbConnect. Every page may not use the database object. But as I’ll show you in a future article, you can declare the object once it’s been compiled so that you will only need to modify the pageDeclare.asp file. That way, the object will be created only when it is used, instead of every time a page is requested.
Want more info on ASP?
Get up to speed with ASP by checking out earlier articles from this column:
- · "Mastering the basics of Active Server Pages"
- · "Communicating with Request and Response objects"
- · "Track your users with session objects"
- · "Add power by using objects within ASP pages"
The next page in the framework is named pagePrep.asp and is shown in Listing C.
This page uses Response.Write to set up a basic HTML page. It outputs the document type, html, head, title, and the starting body tag. Notice it uses the page-level variables to control whether the cache information is written for this page. This allows an individual page to set the gExpirePage variable and control the caching of the page.
The gExpirePage variable defaults to true since most ASP pages need the cache turned off to present the most recent information. I should also explain that I set the headers for the cache control and output the meta tags. Just a little extra effort to make sure aggressive proxy servers don’t cache the pages.
The last page of the framework is pageClose.asp. This page contains only the closing body and closing HTML tags. It appears here:
A better page from the framework
Now that the pieces of the framework are defined, take a look at the improved musicList.asp file in Listing D.
Notice that the musicList.asp page looks much cleaner with the framework and the database object. The entire page really consists of only the logic related to the page. All of the standard elements have been moved into three include statements and a few variables that are set at the top of the page. You can make this framework similar to the way the new ASP.NET functions by adding methods that are called for the page instead of straight code. Since ASP.NET has a Page_Load procedure that is called when the page is requested, you could define that procedure as part of the page template and call subprocedures from the framework to build the page, as well as calling methods on the page that contain logic of that page. This is the topic for another article, but I thought I should mention it, in case you’re interested in further modifications.
This framework is a handy tool for quickly developing a site. By using the include statement, you can move the majority of the user interface away from the logic of the pages. This framework is not perfect, but it should be more than adequate for a majority of your projects. In the next article, I’ll show you the database object that completes the rewrite of the musicList.asp file.