Server Side Includes (SSI) with ASP


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"
%>
<!--#include file="../includes/someinclude.asp"-->
<%
Response.write "done executing included code"
%>

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

<%
Response.Write "executing included code now"
%>
<!--#include virtual="/includes/someinclude.asp"-->
<%
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")
%>
<!--#include file="../includes/<%=page%>.asp"-->

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.

Rob Collyer, experienced with 20 years programming knowledge and site administrator of www.webforumz.com - Copyright 2003-2004


MORE RESOURCES:
RELATED ARTICLES
Study Your Sites Performance Via Hit Tracking
Knowledge is power.And undoubtedly, it is the key to making a substantial fortune online.
4 Marketing Tips for Resourceful Webmasters!
The internet is a sea of knowledge. Getting your information to 'float' by the right audience can be like finding that one special grain of sand on the seashore.
Tips for Building a Great Business Website
A website is lifeline of internet home business. In order to succeed you must have a website that that meets the general criteria of accepted standards.
Websites: You Get What You Pay For!
So you finally decided to invest in a web presence or upgrade your current site? There are many available choices in "web designers" to choose from. This decision is an important one! You may have a relative or friend of a friend who will do your web site at a very low cost from the comfort of their house.
Wheres Your Focus, Money or Customer?
Is your website Really useful?I had a 'rep' in yesterday who was telling me he had a website, not realising I'm a man of many talents he was telling me about all the new companies he was working with and the great products he could offer and reminded me to go to his website.When he'd finished, I asked him:-"what is the purpose of your website?" a question which he couldn't answer.
Do This One Thing to Create a Powerful Product-Selling Web Site
Spend time planning your Web site. Before you design a page or write a word, get a visual, and mental picture of your preferred audience.
Creating Dynamic Website Content with PHP - MySQL
Fresh website content for your visitors can be of real benefit when attempting to generate repeat traffic. Most webmasters, however, just don't have enough spare time to frequently update or rebuild their pages manually.
You Need To Treat Your Website Like An Employee - Heres 5 Ways To Do It
People often come to me in a state of crisis and say their website isn't working. Usually they're in a cold sweat:"Mike, give me some practical things I can do to get more sales from my website!"You want practical advice? Stop treating your website like an object in your business and start treating it like an employee.
Flash Deadly Sins (That Can Kill Your Web Business)
Looks like every client wants a Flash site these days but the reality is..
7 Ways to Gain a Professional Online Reputation
A professional online reputation is essential to the flourishing of any business big or small. Many business owners spend countless hours on the core operations of their business forgetting the importance their website has.
The Web: Your Window of Opportunity
Thinking about starting up a business? The web is the way to go. Look around! More and more, people are relying on the internet as an information resource, a way to foster personal relationships, and method of purchasing goods and services.
CPanel Fantastico - Features and Benefits
Fantastico is a 3rd-party cPanel add-on and can be purchased and added to the standard cPanel interface. Fantastico allows you to quickly install a variety of open-source scripts.
Building Websites with Directory Generator
Directory Generator, a product from Armand Morin and Marc Quarles, builds directory-style websites for you in a matter of minutes. It is simple to use, it's reliable, it does what it claims to do.
How To Write More Powerful Online Text
Although there are significant differences among the various types of online communication, there all have one critical thing in common - they're read off a screen. There are substantial benefits, too, in that while your message is on someone's screen usually it has their undivided attention.
Why Should Your Business Go O.N.L.I.N.E.
Optimize your time. How much time each day must you devote to paperwork, scheduling and accounting? You strive to meet the needs of your clients, but when chained to the obligations of business management, it's just as important to make sure the doors stay open as it is to satisfy customers.
New Years Resolutions: Is Improving Your Website One of Them?
With the New Year upon us yet again, it's time to prepare for the successes of 2005. Did you watch with envy last year as your competitors dominated your industry? Do you think your site is doing just fine because no one has ever called to complain? Well here's some food for thought: 96% of all prospects will click over to your competitor if they encounter a problem on your website.
Web Design for the Professional Magician Part I - Selecting the Perfect Domain Name
Selecting the Perfect Domain NameThe first issue I have with magician web sites is the improper use of domain names. Examine the following list of URLs and select the most professional one: www.
A Website Is Not Enough
Your ObjectiveYour small business needs an appealing and professional website but that is just the beginning. If no one visits your website and -- more importantly -- if it does not bring in new customers and allows you to sell more to your current customers, then your site is not effective.
Getting a Web Site Can Be Fast, Easy, and Inexpensive When You Know Your Options
So what do you do if need to get a web site but you're not sure how to go about getting one. The good news is you have several options.
Dreamweaver 8 Preview
August 8 2005, Macromedia announced a release of Studio 8. Based on the feedback of previous users, Macromedia made it's already very powerful software even better.