服务器端包含有什么方法可以获取包含该文件的文件名或 URL?

发布于 2024-09-05 04:06:21 字数 1270 浏览 2 评论 0原文

服务器端包含有什么方法可以确定它被包含在哪个文件中?我想做这样的事情:

<ul id="menu">
    <!--#if expr="$URL = index.html" -->
        <li><span>Home</span></li>
    <!--#else -->
        <li><a href="index.html">Home</a></li>
    <!--#endif -->
    <!--#if expr="$URL = about.html" -->
        <li><span>About us</span></li>
    <!--#else -->
        <li><a href="about.html">About us</a></li>
    <!--#endif -->
    <!--#if expr="$URL = contact.html" -->
        <li><span>Contact us</span></li>
    <!--#else -->
        <li><a href="contact.html">Contact us</a></li>
    <!--#endif -->
</ul>

我只需要知道是否可以使用服务器端包含,如果可以,语法是什么。

我对服务器端包含做了一些研究,并尝试了这样的事情:

<!--#if expr="DOCUMENT_URI = /contact.html" -->
    this is the contact page
<!--#endif -->

我在输出中看到了这一点:

[处理此指令时发生错误]这是联系页面

就像它正在理解命令一样,但由于某种原因某些东西导致了错误。

我尝试使用 SCRIPT_FILENAME、REQUEST_URI、SCRIPT_NAME 和 DOCUMENT_NAME 代替 DOCUMENT_URI 进行类似的操作,无论有或没有所需的路径信息,在每种情况下,我都能使其工作,但我收到了该错误消息。有谁知道什么会导致这种情况?

Is there any way for a server-side include to determine what file it is being included in? I would like to do something like this:

<ul id="menu">
    <!--#if expr="$URL = index.html" -->
        <li><span>Home</span></li>
    <!--#else -->
        <li><a href="index.html">Home</a></li>
    <!--#endif -->
    <!--#if expr="$URL = about.html" -->
        <li><span>About us</span></li>
    <!--#else -->
        <li><a href="about.html">About us</a></li>
    <!--#endif -->
    <!--#if expr="$URL = contact.html" -->
        <li><span>Contact us</span></li>
    <!--#else -->
        <li><a href="contact.html">Contact us</a></li>
    <!--#endif -->
</ul>

I just need to know if it is possible using server-side includes, and if so, what the syntax would be.

I did some research on server-side includes and tried something like this:

<!--#if expr="DOCUMENT_URI = /contact.html" -->
    this is the contact page
<!--#endif -->

And I see this in the output:

[an error occurred while processing this directive] this is the contact page

like it is understanding the command but something is causing an error for some reason.

I tried similar things with SCRIPT_FILENAME, REQUEST_URI, SCRIPT_NAME, and DOCUMENT_NAME in place of DOCUMENT_URI, with or without the required path information, and in each case, I was able to make it work but I got that error message. Does anyone have any idea what would cause this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深海少女心 2024-09-12 04:06:26

我想通了:

<!--#if expr='"$DOCUMENT_URI" = "/contact.html"' -->
    This is the contact page.
<!--#endif -->

I figured it out:

<!--#if expr='"$DOCUMENT_URI" = "/contact.html"' -->
    This is the contact page.
<!--#endif -->
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文