Home / code / asp

SSI and Subroutines/functions,

SSI or Server Side Includes, are used to make programming more transparent, it is not limited to ASP. Instead of write all the code in every page, you can write common code in a separated file that is called in every file where the code is needed.

Be careful, asp pages can not be read by outsiders, SSI-files can (if they know the right file name).

Example : product1.asp

<HTML>
<HEAD><TITLE>Product tour</TITLE></HEAD>
<BODY>
<FORM>
<H1>Product1</H1>

<%
dim pNext,pPrev,objNL,dNext,dPrev,listindex,listcount,pFirst,pEnd
set objNL=server.createobject("MSWC.NextLink")
pPrev=objNL.GetPreviousURL("producttour.txt")
pNext=objNL.GetNextURL("producttour.txt")
dNext=objNL.GetNextDescription("producttour.txt")
listindex=objNL.GetListIndex("producttour.txt")
listcount=objNL.GetListCount("producttour.txt")
pFirst=objNL.GetNthURL("producttour.txt",1)
pENd=objNL.GetNthURL("producttour.txt",listcount)
%>
<% if listindex > 1 then%>
<input type=button value='<% =dPrev %>' onClick="location.href='<%=pPrev %>'">
<% end if %>
<% if listindex <> listcount then%>
<input type=button value='<% =dNext %>' onClick="location.href='<%=pNext %>'">
<% end if %>
<input type=button value=home onClick="location.href='< =pFirst %>'">
<input type=button value=end onClick="location.href='< =pEnd %>'">
<input type=button value=TOC onClick="location.href='toc.asp'">

</FORM>
</BODY>
</HTML>

product2.asp

<HTML>
<HEAD><TITLE>Product tour</TITLE></HEAD>
<BODY>
<FORM>
<H1>Product2</H1>

<%
dim pNext,pPrev,objNL,dNext,dPrev,listindex,listcount,pFirst,pEnd
set objNL=server.createobject("MSWC.NextLink")
pPrev=objNL.GetPreviousURL("producttour.txt")
pNext=objNL.GetNextURL("producttour.txt")
dNext=objNL.GetNextDescription("producttour.txt")
listindex=objNL.GetListIndex("producttour.txt")
listcount=objNL.GetListCount("producttour.txt")
pFirst=objNL.GetNthURL("producttour.txt",1)
pENd=objNL.GetNthURL("producttour.txt",listcount)
%>
<% if listindex > 1 then%>
<input type=button value='<% =dPrev %>' onClick="location.href='<%=pPrev %>'">
<% end if %>
<% if listindex <> listcount then%>
<input type=button value='<% =dNext %>' onClick="location.href='<%=pNext %>'">
<% end if %>
<input type=button value=home onClick="location.href='< =pFirst %>'">
<input type=button value=end onClick="location.href='< =pEnd %>'">
<input type=button value=TOC onClick="location.href='toc.asp'">

</FORM>
</BODY>
</HTML>

and so on for product3.asp, product4.asp, ...

Now when can place the code to make the buttons in a separated file : buttons.inc

<%
dim pNext,pPrev,objNL,dNext,dPrev,listindex,listcount,pFirst,pEnd
set objNL=server.createobject("MSWC.NextLink")
pPrev=objNL.GetPreviousURL("producttour.txt")
pNext=objNL.GetNextURL("producttour.txt")
dNext=objNL.GetNextDescription("producttour.txt")
listindex=objNL.GetListIndex("producttour.txt")
listcount=objNL.GetListCount("producttour.txt")
pFirst=objNL.GetNthURL("producttour.txt",1)
pENd=objNL.GetNthURL("producttour.txt",listcount)
%>
<% if listindex > 1 then%>
<input type=button value='<% =dPrev %>' onClick="location.href='<%=pPrev %>'">
<% end if %>
<% if listindex <> listcount then%>
<input type=button value='<% =dNext %>' onClick="location.href='<%=pNext %>'">
<% end if %>
<input type=button value=home onClick="location.href='< =pFirst %>'">
<input type=button value=end onClick="location.href='< =pEnd %>'">
<input type=button value=TOC onClick="location.href='toc.asp'">

You call the file buttons.inc with the following line:
<!-- #include file="buttons.inc" -->

product1.asp looks then like:

<HTML>
<HEAD><TITLE>Product tour</TITLE></HEAD>
<BODY>
<FORM>
<H1>Product1</H1>

<!-- #include file="buttons.inc" -->

</FORM>
</BODY>
</HTML>

 

SSI and subroutines/functions

The great advantage of SSI is that all code is concentrated in one place and only have to be written once.

The example below shows how we can use the code also for other tutorials.

producttour.txt

product1.asp	product1	product1 comment
product2.asp	product2	product2 comment
product3.asp	product3	product3 comment
product4.asp	product4	product4 comment
product5.asp	product5	product5 comment
tocc.asp	toc	toc comment

buttons.inc

<script language=vbscript runat=server>
SUB buttons(list)
  dim pNext,pPrev,objNL,dNext,dPrev,listindex,listcount,pFirst,pEnd
  set objNL=server.createobject("MSWC.NextLink")
  pPrev=objNL.GetPreviousURL("list")
  pNext=objNL.GetNextURL("list")
  dNext=objNL.GetNextDescription("list")
  listindex=objNL.GetListIndex("list")
  listcount=objNL.GetListCount("list")
  pTOC=objNL.GetNthURL(list,listcount)
  listcount=listcount-1
  pFirst=objNL.GetNthUrl(list,1)
  pEnd=objNL.GetNthURL(list,listcount)

  if listindex >1 then
	response.write("<input type=button value='"+dPrev+"' onClick=""locaztion.href='"+pPrev"'"">")
  end if
  if listindex <> listcount then
	response.write("<input type=button value='"+dNext+"' onClick=""locaztion.href='"+pNext"'"">")
  end if
  response.write("<input type=button value=home onClick=""locaztion.href='"+pFirst"'"">")
  response.write("<input type=button value=end onClick=""locaztion.href='"+pEND"'"">")
  response.write("<input type=button value=TOC onClick=""locaztion.href='"+pTOC"'"">")
END Sub

Then we get the following result:

<HTML>
<HEAD><TITLE>Product tour</TITLE></HEAD>
<BODY>
<FORM>
<H1>Product 1</H1>
<%call buttons("producttour.txt") %>
</FORM>
</BODY>
</HTML>
<!-- #include file="buttons.inc" -->

 

TOP

Latest script:

 

Books: