📄 ssi.html.en
字号:
</pre><hr><h2><a name="additionalexamples">Additional examples</a></h2><p>Following are some specific examples of things you can do in yourHTML documents with SSI.</p><hr><h2><a name="whenwasthisdocumentmodified">When was this documentmodified?</a></h2><p>Earlier, we mentioned that you could use SSI to inform the user whenthe document was most recently modified. However, the actual method fordoing that was left somewhat in question. The following code, placed inyour HTML document, will put such a time stamp on your page. Of course,you will have to have SSI correctly enabled, as discussed above.</p><pre> <!--#config timefmt="%A %B %d, %Y" --> This file last modified <!--#flastmod file="ssi.shtml" --></pre><p>Of course, you will need to replace the <code>ssi.shtml</code> withthe actual name of the file that you're referring to. This can beinconvenient if you're just looking for a generic piece of code thatyou can paste into any file, so you probably want to use the<code>LAST_MODIFIED</code> variable instead:</p><pre> <!--#config timefmt="%D" --> This file last modified <!--#echo var="LAST_MODIFIED" --></pre><p>For more details on the <code>timefmt</code> format, go to yourfavorite search site and look for <code>ctime</code>. The syntax is thesame.</p><hr><h2><a name="includingastandardfooter">Including a standardfooter</a></h2><p>If you are managing any site that is more than a few pages, you mayfind that making changes to all those pages can be a real pain,particularly if you are trying to maintain some kind of standard lookacross all those pages.</p><p>Using an include file for a header and/or a footer can reduce theburden of these updates. You just have to make one footer file, andthen include it into each page with the <code>include</code> SSIcommand. The <code>include</code> element can determine what file toinclude with either the <code>file</code> attribute, or the<code>virtual</code> attribute. The <code>file</code> attribute is afile path, <em>relative to the current directory</em>. That means thatit cannot be an absolute file path (starting with /), nor can itcontain ../ as part of that path. The <code>virtual</code> attribute isprobably more useful, and should specify a URL relative to the documentbeing served. It can start with a /, but must be on the same server asthe file being served.</p><pre> <!--#include virtual="/footer.html" --></pre><p>I'll frequently combine the last two things, putting a<code>LAST_MODIFIED</code> directive inside a footer file to beincluded. SSI directives can be contained in the included file, andincludes can be nested - that is, the included file can include anotherfile, and so on.</p><hr><h2><a name="whatelsecaniconfig">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 getthe message</p><pre> [an error occurred while processing this directive]</pre><p>If you want to change that message to something else, you can do sowith the <code>errmsg</code> attribute to the <code>config</code>element:</p><pre> <!--#config errmsg="[It appears that you don't know how to use SSI]" --></pre><p>Hopefully, end users will never see this message, because you willhave resolved all the problems with your SSI directives before yoursite goes live. (Right?)</p><p>And you can <code>config</code> the format in which file sizes arereturned 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><hr><h2><a name="executingcommands">Executing commands</a></h2><p>I expect that I'll have an article some time in the coming monthsabout using SSI with small CGI programs. For now, here's something elsethat you can do with the <code>exec</code> element. You can actuallyhave SSI execute a command using the shell (<code>/bin/sh</code>, to beprecise - or the DOS shell, if you're on Win32). The following, forexample, will give you a directory listing.</p><pre> <pre> <!--#exec cmd="ls" --> </pre></pre><p>or, on Windows</p><pre> <pre> <!--#exec cmd="dir" --> </pre></pre><p>You might notice some strange formatting with this directive onWindows, 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 executewhatever code happens to be embedded in the <code>exec</code> tag. Ifyou have any situation where users can edit content on your web pages,such as with a ``guestbook'', for example, make sure that you have thisfeature 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><hr><h2><a name="advancedssitechniques">Advanced SSI techniques</a></h2><p>In addition to spitting out content, Apache SSI gives you the optionof setting variables, and using those variables in comparisons andconditionals.</p><h3><a name="caveat">Caveat</a></h3><p>Most of the features discussed in this article are only available toyou if you are running Apache 1.2 or later. Of course, if you are notrunning Apache 1.2 or later, you need to upgrade immediately, if notsooner. Go on. Do it now. We'll wait.</p><hr><h2><a name="settingvariables">Setting variables</a></h2><p>Using the <code>set</code> directive, you can set variables forlater use. We'll need this later in the discussion, so we'll talk aboutit here. The syntax of this is as follows:</p><pre> <!--#set var="name" value="Rich" --></pre><p>In addition to merely setting values literally like that, you canuse any other variable, including, for example, environment variables,or some of the variables we discussed in the last article (like<code>LAST_MODIFIED</code>, for example) to give values to yourvariables. You will specify that something is a variable, rather than aliteral string, by using the dollar sign ($) before the name of thevariable.</p><pre> <!--#set var="modified" value="$LAST_MODIFIED" --></pre><p>To put a literal dollar sign into the value of your variable, youneed to escape the dollar sign with a backslash.</p><pre> <!--#set var="cost" value="\$100" --></pre><p>Finally, if you want to put a variable in the midst of a longerstring, and there's a chance that the name of the variable will run upagainst some other characters, and thus be confused with thosecharacters, you can place the name of the variable in braces, to removethis confusion. (It's hard to come up with a really good example ofthis, but hopefully you'll get the point.)</p><pre> <!--#set var="date" value="${DATE_LOCAL}_${DATE_GMT}" --></pre><hr><h2><a name="conditionalexpressions">Conditional expressions</a></h2><p>Now that we have variables, and are able to set and compare theirvalues, we can use them to express conditionals. This lets SSI be atiny programming language of sorts. <code>mod_include</code> providesan <code>if</code>, <code>elif</code>, <code>else</code>,<code>endif</code> structure for building conditional statements. Thisallows you to effectively generate multiple logical pages out of oneactual page.</p><p>The structure of this conditional construct is:</p><pre> <!--#if expr="test_condition" --> <!--#elif expr="test_condition" --> <!--#else --> <!--#endif --></pre><p>A <em>test_condition</em> can be any sort of logical comparison -either comparing values to one another, or testing the ``truth'' of aparticular value. (A given string is true if it is nonempty.) For afull list of the comparison operators available to you, see the<code>mod_include</code> documentation. Here are some examples of howone might use this construct.</p><p>In your configuration file, you could put the following line:</p><pre> BrowserMatchNoCase macintosh Mac BrowserMatchNoCase MSIE InternetExplorer</pre><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><pre> <!--#if expr="${Mac} && ${InternetExplorer}" --> Apologetic text goes here <!--#else --> Cool JavaScript code goes here <!--#endif --></pre><p>Not that I have anything against IE on Macs - I just struggled for afew hours last week trying to get some JavaScript working on IE on aMac, when it was working everywhere else. The above was the interimworkaround.</p><p>Any other variable (either ones that you define, or normalenvironment variables) can be used in conditional statements. WithApache's ability to set environment variables with the<code>SetEnvIf</code> directives, and other related directives, thisfunctionality can let you do some pretty involved dynamic stuff withoutever resorting to CGI.</p><hr><h2><a name="conclusion">Conclusion</a></h2><p>SSI is certainly not a replacement for CGI, or other technologiesused for generating dynamic web pages. But it is a great way to addsmall amounts of dynamic content to pages, without doing a lot of extrawork.</p></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -