基于 URI 的条件 SSI

发布于 2024-09-16 09:08:45 字数 576 浏览 1 评论 0原文

我正在重做包含我网站的几十个页面的导航栏。我想要做出的更改之一是根据用户所在的页面来改变所包含的内容。因此,我的计划基本上是让当前包含在各处的文件充当选择器来决定要包含哪些实际文件。

从我对 Apache SSI 规范 的阅读来看,我相信我可以通过驱动我的 SSI 的条件表达式来执行此操作。类似这样:

<!--#if expr="[URI is page1.shtml]" -->
<!--#include virtual="page1contents.shtml" -->
<!--#elif expr="[URI is page2.shtml]" -->
<!--#include virtual="page2contents.shtml" -->
<!--#endif -->

那么,我的问题是,应该在其中的 [URI is page1] 部分中添加什么内容来实际测试我感兴趣的条件?

I am redoing the navigation bar that is included several dozen of my site's pages. One of the changes that I want to make is to vary the content that is included, based on the page that the user is on. Thus my plan is basically to have the file that is currently included everywhere act as a selector to decide which actual file(s) to include.

From the my reading of the Apache SSI Specs, I believe I can do this through conditional expressions driving my SSI. Something like:

<!--#if expr="[URI is page1.shtml]" -->
<!--#include virtual="page1contents.shtml" -->
<!--#elif expr="[URI is page2.shtml]" -->
<!--#include virtual="page2contents.shtml" -->
<!--#endif -->

My question, then, is what should go in the [URI is page1] part of that to actually test the condition I'm interested in?

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

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

发布评论

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

评论(2

爺獨霸怡葒院 2024-09-23 09:08:45

mod_include 的详细信息中找到了答案:

下面的示例将打印“in foo”
如果 DOCUMENT_URI 是 /foo/file.html,
如果是 /bar/file.html 则为“in bar”并且
否则“两者都不”:

<!--#if expr='"$DOCUMENT_URI" = "/foo/file.html"' -->
in foo
<!--#elif expr='"$DOCUMENT_URI" = "/bar/file.html"' -->
in bar
<!--#else -->
in neither
<!--#endif -->

Found the answer in the details of mod_include:

The below example will print "in foo"
if the DOCUMENT_URI is /foo/file.html,
"in bar" if it is /bar/file.html and
"in neither" otherwise:

<!--#if expr='"$DOCUMENT_URI" = "/foo/file.html"' -->
in foo
<!--#elif expr='"$DOCUMENT_URI" = "/bar/file.html"' -->
in bar
<!--#else -->
in neither
<!--#endif -->
厌味 2024-09-23 09:08:45

SSI expr 仍然可以在使用 v 函数或 % 的现代服务器上工作(在 Apache/2.4.18 上测试)。您还可以使用文档名称。

示例(对文档名称进行 if-test):

<!--#if expr='v("DOCUMENT_NAME")=~/about.html/'-->
<a class="active" href="#">
<!--#else -->
<a href="about.html">
<!--#endif -->
About</a>

示例 2(对文档路径进行 if-test):

<!--#if expr="%{DOCUMENT_URI} =~ /product/"-->
Product path
<!--#else-->
Some other path
<!--#endif-->

SSI expr still works on modern servers using the v function or % (tested on Apache/2.4.18). You can also use the document name.

Example (if-test on document name):

<!--#if expr='v("DOCUMENT_NAME")=~/about.html/'-->
<a class="active" href="#">
<!--#else -->
<a href="about.html">
<!--#endif -->
About</a>

Example 2 (if-test on document path):

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