Digital Marketing

Server Side Includes (SSI) with ASP

Server Side Includes (SSI) with ASP

People often have identical sections of code spread across many ASP pages, typically the code at the top of the page (usually called the header) and the bottom 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

answer.write “YES!”

Plus

answer.write “NO YES!”

It will end if

%>

Ok, ok… it’s simple, but imagine you used this on multiple pages. What is wrong with this picture?

Well… you don’t have to think about it very long before you realize that if you ever wanted to change something about that piece of code, you would have to do a lot of work. you would have to change every page…everyone has done this at some point and luckily most follow the steps to learn how to fix this problem.

If we could save our precious piece of code in a file and have access to that file whenever we needed the code, then we would no longer have problems when it comes to changing things, since we just update a file. File Include or Server Side Includes (SSI), as they are more commonly known, is a powerful way to not only save time when it comes to maintaining and updating a site, but also to increase the efficiency and speed of that site.

Ok, so let’s see how we use an include file… to get started, we need to use one of two methods. The former allows you to include files relatively. By that I mean relative to the directory in which the ASP file that uses 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) that includes another file (/includes/someinclude.asp).

To demonstrate relative inclusion, look at this:

<%

Reply. Type “running bundled code now”

%>

<%

Response.write “made to execute the included code”

%>

And here, we give an example of the other method, virtual inclusion:-

<%

Reply. Type “running bundled code now”

%>

<%

Response.write “made to execute the included code”

%>

Note the slight difference between the two methods. With virtual include, you can move the file that calls the include file anywhere without affecting the result, because the path is always relative to root. However, the same cannot be said of relative inclusion. If you move a file that uses relative inclusion to a different directory, you will most likely find that the relative path is no longer correct.

From the above examples, you may also notice that both methods, when calling the include, call OUTSIDE the ASP script block. This is because ASP does not interpret #include statements. Include files are included before a single ASP line is processed, which means the following will NOT work:

%lt;%

Page=request.form(“page”)

%>

We were trying to do a dynamic include and I’d be the first to admit that the above would be very helpful if it were possible. I intend to cover a few methods that work around this issue, but I know by now you’ll be eager to start using include files right away to worry about this, so I’ll cover it in a future article.

I hope you found the above informative and I hope it inspires you all to write code that is more efficient and easier to maintain.

Leave a Reply

Your email address will not be published. Required fields are marked *