Alazanto

Fun with Related Links

Filed Under: Dynamic Technologies, Journal.

I learned a really neat trick today while working on a IIS hosted site. This is a static site that relies heavily upon server side includes to make updating links a little easier. Recently, the webmaster wanted to be able to add related links to each page where such an addition made sense.

Unfortunately, aside from the page title and body content, the totality of a page’s links are stored in central files. One way to solve this dilema is to embed these links in the body content. However, for this layout, right aligned sidebars are reserved for commentary and photography. These links needed to reside in the left column, alongside the centrally stored links. In addition to this, not every page was to contain related links.

After some thought, I found a solution through the use of a simple ASP script. The goal for this script was to perform the following:

The webmaster would create a new text file with the exact same name, but different extension than the related .asp page. For instance “default.txt” would accompany “default.asp.” Inside this text file would reside the anchor point (URL) and the descriptive string divided by one tab. If this file exists, the script will create an unordered list of links from the content in the text file. If the file does not exist, no placeholder for these links will be visible.

Our first step involves capturing the current file name (for this example, default.asp) from the virtual path.

	
Dim virtualPath
Dim fileName
	
virtualPath = Request.ServerVariables(\"SCRIPT_NAME\")
fileName = Split(virtualPath, \"/\", -1, 1)
	

Now we want give the captured name a new file extension, since we are going to be searching for a file called (in this example) default.txt.

	
Dim addON
	
addOn = (Replace(fileName(ubound(fileName)), \".asp\", \".txt\"))
	

At this point, we need to query for the full server path and attach this path to the new file name.

	
Dim fullPath
	
fullPath = Server.MapPath(addOn)
	

Once this step is completed, we want the script to see if the path to the hypothetical text file actually exists.

	
Set fs=Server.CreateObject(\"Scripting.FileSystemObject\")
	
If (fs.FileExists(fullPath)) = true Then
	

If the file exists we can create a loop that parses the text file and constructs a unordered list of linked items.

	
  dim c ' counter
  dim i ' increment
	
  set nl=server.createobject(\"MSWC.Nextlink\")
	
  c = nl.GetListCount(addOn)
  i = 1
	
  response.write(\"<ul>\")
	
  do while (i < = c)
	
    response.write(\"<li><a href='\" & nl.GetNthURL(addOn, i) & \"'>\")
	
    response.write(nl.GetNthDescription(addOn, i) & \"
	

If the file does not exist, you can choose for the script to print out a message, or do nothing.

	
Else
	
End If
	
set fs = nothing
	

Lastly, here is an example of the .txt file’s contents.

	
contact.aspContact Us!
info.aspFind more Information
	

Additionally, here is all the code, neatly packed, and ready to copy and paste.

	
< %
	
' grab the current file name
	
Dim virtualPath
Dim fileName
Dim addOn
Dim fullPath
	
virtualPath = Request.ServerVariables(\"SCRIPT_NAME\")
fileName = Split(virtualPath, \"/\", -1, 1)
	
addOn = (Replace(fileName(ubound(fileName)), \".asp\", \".txt\"))
	
' create the full server path
	
fullPath = Server.MapPath(addOn)
	
' if the full server path and file exist on the server, grab the content from the file
	
Set fs=Server.CreateObject(\"Scripting.FileSystemObject\")
	
If (fs.FileExists(fullPath)) = true Then
	
  dim c ' counter
  dim i ' increment
	
  set nl=server.createobject(\"MSWC.Nextlink\")
	
  c = nl.GetListCount(addOn)
  i = 1
	
  ' now turn the contents into a nice unordered list
	
  response.write(\"<h3>Related information</h3>\")
	
  response.write(\"<ul>\")
	
  ' here is the loop that does all the dirty work
	
  do while (i <= c)
	
    response.write(\"<li><a href='\" & nl.GetNthURL(addOn, i) & \"'>\")
	
    response.write(nl.GetNthDescription(addOn, i) & \"</a></li>\")
	
  i = (i + 1)
  loop
	
  response.write(\"</ul>\")
	
Else
	
' if the file doesn't exist do nothing
	
End If
	
set fs = nothing
	
%>
	

Published: 4 years ago