Classic ASP โ #include โ Reuse Code Across Pages with Server-Side Includes
Introduction โ What Is #include in Classic ASP?
In Classic ASP, the #include directive is used to reuse code across multiple .asp pages. This is known as a Server-Side Include (SSI) and it allows you to place shared functions, HTML headers, footers, or configuration in a single file and include it wherever needed.
In this guide, youโll learn:
- How to use
#includefor code reusability - Difference between virtual and file includes
- Best practices for layout, functions, and error prevention
- Examples of common uses like headers, footers, and constants
Syntax of #include
<!-- #include virtual="path" -->
<!-- #include file="relative_path" -->
These lines must be placed outside of <% %> script blocks.
#include file vs #include virtual
| Directive | Path Type | Example |
|---|---|---|
#include file | Relative path | <!--#include file="includes/footer.asp"--> |
#include virtual | Absolute path (from site root) | <!--#include virtual="/includes/header.asp"--> |
Example โ Including a Header and Footer
/includes/header.asp
<h1>My Website Header</h1>
<hr>
/includes/footer.asp
<hr>
<p>ยฉ 2025 My Website</p>
/index.asp
<!--#include virtual="/includes/header.asp"-->
<p>Welcome to the homepage!</p>
<!--#include virtual="/includes/footer.asp"-->
Output:
<h1>My Website Header</h1>
-------------------------
Welcome to the homepage!
-------------------------
ยฉ 2025 My Website
Reusing Functions with #include
mathlib.asp
<%
Function Add(x, y)
Add = x + y
End Function
%>
main.asp
<!--#include file="mathlib.asp"-->
<%
Dim result
result = Add(5, 3)
Response.Write "Sum: " & result
%>
Output: Sum: 8
Rules and Limitations
| Rule | Explanation |
|---|---|
Must be outside <% %> tags | ASP will error otherwise |
No dynamic includes (#include can’t use variables) | All paths must be static |
| No conditional includes | Includes are parsed before execution |
Common Uses of #include
- Site-wide headers and footers
- Shared constants or config settings
- Database connection file
- Utility functions or error handlers
Best Practices for Classic ASP #include
Do:
- Use
virtualfor root-relative includes across folders - Use
filefor including files within the same directory - Keep reusable logic (functions, constants) in
.aspinclude files
Avoid:
- Deep nested includes
- Including large blocks of unrelated logic
- Placing
#includeinside script tags
Summary โ Recap & Next Steps
The #include directive in Classic ASP is a powerful feature that promotes modular, maintainable code. Whether you’re building a layout or reusing functions, #include keeps your project DRY (Don’t Repeat Yourself).
Key Takeaways:
- Use
#include fileand#include virtualto reuse HTML or VBScript - Includes must be static and outside ASP blocks
- Perfect for headers, footers, config, and utility code
Real-world Use Cases:
- Add site-wide navigation and branding
- Centralize database connection logic
- Maintain consistency across 100s of
.asppages
FAQs โ Classic ASP #include
Can I use a variable in an #include path?
No. #include paths must be static stringsโthey are evaluated before ASP code runs.
What’s the difference between file and virtual?
file uses a relative path, while virtual starts from the site root (IIS virtual directory).
Can I place #include inside <% %>?
No. Includes must be declared outside ASP script blocks.
Share Now :
