๐Ÿ’ป Classic ASP โ€“ Logic & Scripting
Estimated reading: 3 minutes 52 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 :

Leave a Reply

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

Share

๐Ÿ“Ž Classic ASP โ€“ #include

Or Copy Link

CONTENTS
Scroll to Top