Server Side Includes (SSI) with ASP
People often have identical sections of code spread throughout many ASP pages, typically code at the top of the page (usually referred to as the header) and the lower part of the page (the footer). Similarly, you may have code that performs a certain set of instructions that you are repeating across multiple pages. Consider an example:-
<%
If foobar="yes" then
response.write "YES!"
Else
response.write "NOT YES!"
End if
%>
Ok, ok... it's simple, but imagine that you used this on multiple pages. What's wrong with this picture?
Well... you don't have to think about that long before realising that if you ever wanted to change something about that piece of code, you'd have your work cut out. you would have to change every page... everyone has done this at some stage and luckily most usually take the steps to learn a way around this problem.
If we could save our valuable chunk of code into a file and have that file accessed whenever we needed the code, then we no longer have a problem when it comes to changing things, as we simply update one file. Include files or Server Side Includes (SSI) as they are more commonly known are a powerful way to not only save time when it comes to maintaining and updating a site, but also increase efficiency and speed of that site too!
Ok, lets see how we use an include file... to start with we need to use one of two methods. The first one allows for including files relatively. By that I mean relative to the directory the ASP file that is using the include file resides. The second method includes files virtually. Including files virtually means that the path is taken from the root directory.
Consider that we are going to have a file (/main/somefile.asp) include another file (/includes/someinclude.asp).
To demonstrate, relative inclusion, look at this:-
<%
Response.Write "executing included code now"
%>
[an error occurred while processing this directive]
<%
Response.write "done executing included code"
%>
And here, we give an example of the other method, virtual inclusion:-
<%
Response.Write "executing included code now"
%>
[an error occurred while processing this directive]
<%
Response.write "done executing included code"
%>
Note the slight difference between the two methods. With virtual inclusion, you can move the file that calls upon the include file anywhere without affecting the outcome, because the path is always relative to the root. The same cannot be said however for relative inclusion. If you move a file that is using relative inclusion to a different directory, you'll most likely find the relative path is no longer correct.
From the examples above, you may also notice that both methods, when calling the include do so OUTSIDE of the ASP script block. This because #include declarations are not interpreted by ASP. Include files are included before a single line of ASP is processed which means the following WONT work:-
%lt;%
Page=request.form("page")
%>
[an error occurred while processing this directive]
We were attempting to perform a dynamic include and I would be the first to admit that the above would be very useful indeed if in fact it were possible. I do intend to cover a few methods that work around this set back but I know you will now be itching to get started right away on using include files to even worry about this, so I will cover it in a future article.
I hope that you have found the above informative and hope it inspires you all to write code that's more efficient and more easily maintained.
Return to Index
Read more:
e Mails Not Getting Answered Shhhh! Heres WhyLet's face it -- none of us like to be ignored. But sending an e-mail to a colleague that commits even ONE of these four cardinal sins can mean the difference between a speedy reply ... and that bi...
How To Use Database Marketing To Skyrocket Your Online ProfitsDatabase Marketing is the gathering and storing of specific information about your prospects or customers. This information is usually stored in a database program on your computer. You would then ...
Ten Ways to Make Your Book Outsell AnotherWouldn't you rather write a book that sells well than be stuck with unsold inventory? When you plan ahead with the 10 tips below, you will sell thousands rather than hundreds of your unique and imp...
What Is This RSS, XML, RDF, and Atom BusinessIt's been a long day at work and you're in no mood to cook dinner or go out. Time to count on the reliable pizza delivery guy. The order is called in and he promptly arrives with smokin' hot pizza ...