• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL

Project 11. Creating Dynamic Navigation ... > Powering the Content Switcher with S...

Powering the Content Switcher with Server.Execute

The fun part of this project is finally here! In this section, you’re going to put together all the code for the content switcher. All this will be done inside contentswitcher.asp inside the includes folder.

Open contentswitcher.asp inside the includes folder in your site, switch to Code view, and delete everything in the file. You need to start with a completely clean slate.


To save you the trouble of deleting everything in a default ASP VBScript page, instead create a new text file that is already empty by default. Just be sure to save the file with an .asp file extension.

Define the strCurrentFolder variable and assign the full page path to it by using Request.ServerVariables("PATH_INFO").

You’ll use the strCurrentFolder variable to check which folder the current page is in so you can display the correct menu to the viewer. The variable will contain the full folder path. So if a page is at http://localhost/dir1/dir2/page.asp, then PATH_INFO will return /dir1/dir2/page.asp.

Listing 11.1.

Dim strCurrentPage
strCurrentPage = Request.ServerVariables("PATH_INFO")

Create an array to define which folders will show which menus. In the array, the folders and menus are listed as folder1, menu1, folder2, menu2, and so on.


If you want an exact folder match, include the leading and trailing backslashes in your folder names. Use /Panels/ to ensure only files in the Panels folder will be matched. If you use Panels, then files in Panels-Large and Panels-Small will both be matched.

Listing 11.2.

Dim arMenus
arMenus = Array("/Panels/","panels.asp","/Portraits/","portraits.asp","/Whites/","whites.asp")


Define the strMenuFolder variable and assign it the value /menus/. Be certain to include both the leading and trailing slashes. This variable will define which folder all your menus are stored in. You need to make sure that this path is relative to the site root so that no matter where the page your viewer is looking at is located, the path to the executed menus will be correct.

Listing 11.3.

Const strMenuFolder = "/menus/"

Define the variable i to do the counting through your array, and define bolFoundMatch to store whether or not you’ve actually found a valid match of a folder name in your array. Set the bolFoundMatch variable to False by default.

Listing 11.4.

Dim i 'Use to loop through our array
Dim bolFoundMatch 'Use to store result of match testing.
bolFoundMatch = False

Use the next line of code to start looping through the arMenus array two items at a time. You move through the array two at a time to do comparisons on every other entry in the array. The even numbered entries (arrays start at 0, not 1) are the folder names, and the odd numbered entries are the menu filenames. In the next step, you’ll do the actual checking.

Listing 11.5.

For i = 0 To UBound(arMenus) Step 2


For/Next loops make it extremely easy to loop through a pre-defined list of values. This statement says to start at the first element (arrays are 0 based) and go to the upper bound (UBound) of the array, stepping through the array two elements at a time.

Use the InStr function to test the strCurrentPage variable, which contains the page path, against the current item in the array.

Listing 11.6.

If InStr(strCurrentPage,arMenus(i)) > 0 Then

To test for a string’s value inside another string, the InStr function takes two arguments: the string to search through (strCurrentPage) and the string you want to look for (arMenus(i)). The InStr function returns the position of the first occurrence of the searched for string. If no match is found, InStr returns 0. So if InStr returns a number greater than 0, you’ve found a match.

If you find a match, you need to do a number of things inside the If statement: use Server.Execute to add the menu to the page, set the bolFoundMatch variable to True, and exit the For loop.

This step is where you’ll see the real power of Server.Execute. There’s absolutely no way those three little lines of code could be accomplished without massive amounts of hand coding using standard includes. The Server.Execute method adds the appropriate menu to the page based on the values you created in your array. To add another menu to the site, just add two more entries into the arMenus array, and you’re set.

Listing 11.7.

    Server.Execute(strMenuFolder & arMenus(i + 1)
    bolFoundMatch = True
    Exit For
  End If

So you can see how it all fits together, here’s the entire code block for steps 6 through 8.

Listing 11.8.

For i = 0 To UBound(arMenus) Step 2
  If InStr(strCurrentPage,arMenus(i)) > 0 Then
    Server.Execute(strMenuFolder & arMenus(i + 1)
    bolFoundMatch = True
    Exit For
  End If

If the checks find no match, either use Server.Execute to pull in a default menu or simply display a message to the viewer.

Listing 11.9.

If bolFoundMatch = False Then
  Response.Write("No Menu Match Found.")
  'or Server.Execute("/menus/menu.asp")
End If

Open nav.asp again, and add the contentswitcher.asp include where you want the dynamic data to display. Place the insertion point where you want the content switcher and choose Insert > HTML > Script Objects > Server-Side Include; browse to the contentswitcher.asp file and click OK.

In the finished files provided with the project, the content switcher is added directly below the links to the individual sections in the site.

Listing 11.10.

<a href="/Whites/Default.asp">Whites</a><br />
<br />
<strong>Gallery Sections</strong><br />
<!--#include virtual="/includes/contentswitcher.asp" -->

Now that you’ve added the content switcher include to the nav.asp include, you’re all done, and your menu switcher should show the appropriate content for each main section of your site.



Not a subscriber?

Start A Free Trial

  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint