๐Ÿ’ป Classic ASP โ€“ Logic & Scripting
Estimated reading: 3 minutes 400 views

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 #include for 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

DirectivePath TypeExample
#include fileRelative path<!--#include file="includes/footer.asp"-->
#include virtualAbsolute 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

RuleExplanation
Must be outside <% %> tagsASP will error otherwise
No dynamic includes (#include can’t use variables)All paths must be static
No conditional includesIncludes 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 virtual for root-relative includes across folders
  • Use file for including files within the same directory
  • Keep reusable logic (functions, constants) in .asp include files

Avoid:

  • Deep nested includes
  • Including large blocks of unrelated logic
  • Placing #include inside 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 file and #include virtual to 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 .asp pages

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

๐Ÿ“Ž Classic ASP โ€“ #include

Or Copy Link

CONTENTS
Scroll to Top