include 指令(引用文件)

一、 #include 指令核心概念

  • 作用:在服务器执行 ASP 文件 之前,将另一个文件的内容 原样插入​ 到当前 ASP 文件中。
  • 用途:用于创建函数、页眉、页脚或需要在多个页面重复使用的元素(代码复用)。
  • 处理时机在脚本执行前​ 完成插入和处理(这是理解后续限制的关键)。

二、 基本语法

引用指令必须放在 HTML 注释标签中(服务器会处理,但浏览器会忽略注释标记本身):

<!--#include virtual="somefilename"-->
<!--#include file="somefilename"-->Code language: HTML, XML (xml)
1. virtual 关键词 —— 虚拟路径
  • 指示以 虚拟目录​ 开始的路径。
  • 示例:若 header.inc 位于虚拟目录 /html 中: <!-- #include virtual ="/html/header.inc" -->
2. file 关键词 —— 相对路径
  • 指示 相对于当前 ASP 文件所在目录​ 的路径。
  • 示例:若当前文件在 html 目录,header.inchtml/headers/ 下: <!-- #include file ="headers/header.inc" -->
  • 注意:路径是相对于 引用文件​ 的,如果引用文件不在该目录中,包含将失效。

三、 直观示例

主文件 mypage.asp
<!DOCTYPE html>
<html>
<body>
<h3>Words of Wisdom:</h3>
<p><!--#include file="wisdom.inc"--></p>
<h3>The time is:</h3>
<p><!--#include file="time.inc"--></p>
</body>
</html>Code language: HTML, XML (xml)
被引用文件 wisdom.inc
"One should never increase, beyond what is necessary,
the number of entities required to explain anything."Code language: PHP (php)
被引用文件 time.inc
<%
    Response.Write(Time)
%>Code language: HTML, XML (xml)
浏览器呈现
<!DOCTYPE html>
<html>
<body>
    <h3>Words of Wisdom:</h3>
    <p>"One should never increase, beyond what is necessary,
    the number of entities required to explain anything."</p>
    <h3>The time is:</h3>
    <p>11:33:42 AM</p>
</body>
</html>Code language: HTML, XML (xml)

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注