📄 ssi.html.en
字号:
<!--#config timefmt="%D" --><br /> This file last modified <!--#echo var="LAST_MODIFIED" --></code></p></div> <p>For more details on the <code>timefmt</code> format, go to your favorite search site and look for <code>strftime</code>. The syntax is the same.</p><h3><a name="standard-footer" id="standard-footer">Including a standard footer</a></h3> <p>If you are managing any site that is more than a few pages, you may find that making changes to all those pages can be a real pain, particularly if you are trying to maintain some kind of standard look across all those pages.</p> <p>Using an include file for a header and/or a footer can reduce the burden of these updates. You just have to make one footer file, and then include it into each page with the <code>include</code> SSI command. The <code>include</code> element can determine what file to include with either the <code>file</code> attribute, or the <code>virtual</code> attribute. The <code>file</code> attribute is a file path, <em>relative to the current directory</em>. That means that it cannot be an absolute file path (starting with /), nor can it contain ../ as part of that path. The <code>virtual</code> attribute is probably more useful, and should specify a URL relative to the document being served. It can start with a /, but must be on the same server as the file being served.</p><div class="example"><p><code> <!--#include virtual="/footer.html" --></code></p></div> <p>I'll frequently combine the last two things, putting a <code>LAST_MODIFIED</code> directive inside a footer file to be included. SSI directives can be contained in the included file, and includes can be nested - that is, the included file can include another file, and so on.</p></div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div><div class="section"><h2><a name="config" id="config">What else can I config?</a></h2> <p>In addition to being able to <code>config</code> the time format, you can also <code>config</code> two other things.</p> <p>Usually, when something goes wrong with your SSI directive, you get the message</p><div class="example"><p><code> [an error occurred while processing this directive]</code></p></div> <p>If you want to change that message to something else, you can do so with the <code>errmsg</code> attribute to the <code>config</code> element:</p><div class="example"><p><code> <!--#config errmsg="[It appears that you don't know how to use SSI]" --></code></p></div> <p>Hopefully, end users will never see this message, because you will have resolved all the problems with your SSI directives before your site goes live. (Right?)</p> <p>And you can <code>config</code> the format in which file sizes are returned with the <code>sizefmt</code> attribute. You can specify <code>bytes</code> for a full count in bytes, or <code>abbrev</code> for an abbreviated number in Kb or Mb, as appropriate.</p> </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div><div class="section"><h2><a name="exec" id="exec">Executing commands</a></h2> <p>I expect that I'll have an article some time in the coming months about using SSI with small CGI programs. For now, here's something else that you can do with the <code>exec</code> element. You can actually have SSI execute a command using the shell (<code>/bin/sh</code>, to be precise - or the DOS shell, if you're on Win32). The following, for example, will give you a directory listing.</p><div class="example"><p><code> <pre><br /> <!--#exec cmd="ls" --><br /> </pre></code></p></div> <p>or, on Windows</p><div class="example"><p><code> <pre><br /> <!--#exec cmd="dir" --><br /> </pre></code></p></div> <p>You might notice some strange formatting with this directive on Windows, because the output from <code>dir</code> contains the string ``<<code>dir</code>>'' in it, which confuses browsers.</p> <p>Note that this feature is exceedingly dangerous, as it will execute whatever code happens to be embedded in the <code>exec</code> tag. If you have any situation where users can edit content on your web pages, such as with a ``guestbook'', for example, make sure that you have this feature disabled. You can allow SSI, but not the <code>exec</code> feature, with the <code>IncludesNOEXEC</code> argument to the <code>Options</code> directive.</p> </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div><div class="section"><h2><a name="advanced" id="advanced">Advanced SSI techniques</a></h2> <p>In addition to spitting out content, Apache SSI gives you the option of setting variables, and using those variables in comparisons and conditionals.</p><h3><a name="caveat" id="caveat">Caveat</a></h3> <p>Most of the features discussed in this article are only available to you if you are running Apache 1.2 or later. Of course, if you are not running Apache 1.2 or later, you need to upgrade immediately, if not sooner. Go on. Do it now. We'll wait.</p><h3><a name="variables" id="variables">Setting variables</a></h3> <p>Using the <code>set</code> directive, you can set variables for later use. We'll need this later in the discussion, so we'll talk about it here. The syntax of this is as follows:</p><div class="example"><p><code> <!--#set var="name" value="Rich" --></code></p></div> <p>In addition to merely setting values literally like that, you can use any other variable, including <a href="../env.html">environment variables</a> or the variables discussed above (like <code>LAST_MODIFIED</code>, for example) to give values to your variables. You will specify that something is a variable, rather than a literal string, by using the dollar sign ($) before the name of the variable.</p> <div class="example"><p><code> <!--#set var="modified" value="$LAST_MODIFIED" --> </code></p></div> <p>To put a literal dollar sign into the value of your variable, you need to escape the dollar sign with a backslash.</p><div class="example"><p><code> <!--#set var="cost" value="\$100" --></code></p></div> <p>Finally, if you want to put a variable in the midst of a longer string, and there's a chance that the name of the variable will run up against some other characters, and thus be confused with those characters, you can place the name of the variable in braces, to remove this confusion. (It's hard to come up with a really good example of this, but hopefully you'll get the point.)</p><div class="example"><p><code> <!--#set var="date" value="${DATE_LOCAL}_${DATE_GMT}" --></code></p></div><h3><a name="conditional" id="conditional">Conditional expressions</a></h3> <p>Now that we have variables, and are able to set and compare their values, we can use them to express conditionals. This lets SSI be a tiny programming language of sorts. <code class="module"><a href="../mod/mod_include.html">mod_include</a></code> provides an <code>if</code>, <code>elif</code>, <code>else</code>, <code>endif</code> structure for building conditional statements. This allows you to effectively generate multiple logical pages out of one actual page.</p> <p>The structure of this conditional construct is:</p><div class="example"><p><code> <!--#if expr="test_condition" --><br /> <!--#elif expr="test_condition" --><br /> <!--#else --><br /> <!--#endif --></code></p></div> <p>A <em>test_condition</em> can be any sort of logical comparison - either comparing values to one another, or testing the ``truth'' of a particular value. (A given string is true if it is nonempty.) For a full list of the comparison operators available to you, see the <code class="module"><a href="../mod/mod_include.html">mod_include</a></code> documentation. Here are some examples of how one might use this construct.</p> <p>In your configuration file, you could put the following line:</p><div class="example"><p><code> BrowserMatchNoCase macintosh Mac<br /> BrowserMatchNoCase MSIE InternetExplorer</code></p></div> <p>This will set environment variables ``Mac'' and ``InternetExplorer'' to true, if the client is running Internet Explorer on a Macintosh.</p> <p>Then, in your SSI-enabled document, you might do the following:</p><div class="example"><p><code> <!--#if expr="${Mac} && ${InternetExplorer}" --><br /> Apologetic text goes here<br /> <!--#else --><br /> Cool JavaScript code goes here<br /> <!--#endif --></code></p></div> <p>Not that I have anything against IE on Macs - I just struggled for a few hours last week trying to get some JavaScript working on IE on a Mac, when it was working everywhere else. The above was the interim workaround.</p> <p>Any other variable (either ones that you define, or normal environment variables) can be used in conditional statements. With Apache's ability to set environment variables with the <code>SetEnvIf</code> directives, and other related directives, this functionality can let you do some pretty involved dynamic stuff without ever resorting to CGI.</p></div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div><div class="section"><h2><a name="conclusion" id="conclusion">Conclusion</a></h2> <p>SSI is certainly not a replacement for CGI, or other technologies used for generating dynamic web pages. But it is a great way to add small amounts of dynamic content to pages, without doing a lot of extra work.</p></div></div><div class="bottomlang"><p><span>Available Languages: </span><a href="../en/howto/ssi.html" title="English"> en </a> |<a href="../ja/howto/ssi.html" hreflang="ja" rel="alternate" title="Japanese"> ja </a> |<a href="../ko/howto/ssi.html" hreflang="ko" rel="alternate" title="Korean"> ko </a></p></div><div id="footer"><p class="apache">Copyright 2007 The Apache Software Foundation.<br />Licensed under the <a href="http://www.apache.org/licenses/LICENSE-2.0">Apache License, Version 2.0</a>.</p><p class="menu"><a href="../mod/">Modules</a> | <a href="../mod/directives.html">Directives</a> | <a href="../faq/">FAQ</a> | <a href="../glossary.html">Glossary</a> | <a href="../sitemap.html">Sitemap</a></p></div></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -