๐ 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 :
