65.9K
CodeProject 正在变化。 阅读更多。
Home

SSI - 服务器端包含...一些棘手的事情

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.91/5 (10投票s)

2003年5月4日

4分钟阅读

viewsIcon

87673

downloadIcon

188

这是什么意思?我认为很多人仍然没有使用SSI - 服务器端包含...

Sample Image - euro2004.jpg

引言

服务器端包含 - 这是什么意思?

我认为很多人仍然没有使用SSI - 服务器端包含,因为他们不知道它的重要性或如何利用这种功能,或者因为他们正处于学习ASP的早期阶段,就像我一样。

我将通过一个简单的例子来解释——当你在Microsoft Word中编写一篇长文档(大约50页),并且需要在每一页中都显示你公司的徽标时,你会怎么做?至少,每个人都知道我们可以使用“页眉和页脚”,所以我们需要把它放在第一页,它将成为文档其余部分的一部分,无需太多工作,也更容易更新。

在创建网站时,我们可以应用相同的逻辑,通常页眉、页脚和其他一些对象总是相同的。通常,我使用一个JavaScript用于页眉,另一个用于页脚;当一个网站由纯HTML构成时,这是一个好方法。

但是,如果你开始使用ASP创建网站,那么你必须为页眉和页脚创建单独的ASP页面(包含页面——你可以使用.asp.inc扩展名保存它们),并将它们包含在你希望的每个页面中。我将尝试以简单的方式解释。

  1. 你可以在任何HTML编辑器中创建一个文件——或者简单地在记事本中创建一个文件,并将其另存为header.asp——你可以放入以下代码,以创建一个用作页眉的ASP页面。
    <html>
    <title>Homepage Euro 2004 </title>
    <head>
     
    <base target="contents">
    </head>
     
    <body topmargin="3" leftmargin="3">
     
    <table border="0" width="100%" bgcolor="#E2E8F5" cellspacing="0" 
             cellpadding="0" height="105" style="border: 1 solid #949494">
      <tr>
        <td width="30%" height="104" valign="middle" align="center">
            <img border="0" src="images/yourlogo.gif" 
                       align="absmiddle" WIDTH="162" HEIGHT="27">
        </td>
        <td width="70%" height="104">
           <img border="0" src="images/yourBannerimage.jpg" 
                          align="right" WIDTH="393" HEIGHT="105">
        </td>
      </tr>
    </table>

    这是页眉文件在我们将其作为包含文件之前的样子。

    Header image

    接下来,我们将准备一个页脚,它将在每个页面中通用,通常它包含版权、公司名称和其他信息。

  2. 现在你创建一个另一个文件,并将其另存为bottom.asp(或footer.asp),并在其中写入以下代码。
    <table cellSpacing="0" cellPadding="0" width="100%" border="0">
     <tr>
      <td width="780" bgColor="#e1e1e1" height="6" colspan="5">
        <p align="center"><font size="1" face="Verdana">© 2003/2004 @
           Euro 2004.org</font>
      </td>
     </tr>
     <tr>
      <td width="132" bgColor="#e1e1e1" height="6"> </td>
      <td width="10" bgColor="#e1e1e1" height="6"> </td>
      <td width="496" bgColor="#e1e1e1" height="6">
       <p align="center"><font size="1" face="Verdana">For any
        further information contact our 
        <a href="mailto:kutuji@sapo.pt">Webmaster</a></font>
       </p>
      </td>
      <td width="10" bgColor="#e1e1e1" height="6"> </td>
      <td width="132" bgColor="#e1e1e1" height="6"> </td>
     </tr>
     <tr>
      <td vAlign="center" width="780" background="images/Fundo03.gif" 
                                        colSpan="5" height="22"> </td>
     </tr>
    </table>
    </body>
    </html>

    你可以看到我们正在编写HTML代码,到目前为止我们还没有编写一行VBScript代码——用于此ASP页面。但是,如果你需要从数据库中获取页眉或页脚的一些数据,那么你可以在同一个页面上使用HTML或脚本语言。

    这是页脚文件在我们将其作为包含文件之前的样子。

    Footer image

    你还可以为左侧准备一些包含文件,通常我们在那里放置网站的导航菜单——允许访问者通过链接访问其他页面,也许下次我会展示如何从数据库获取导航菜单的链接。我已经尝试过了,它非常酷,因为我们可以插入和删除,甚至更新整个网站的链接,而无需在所有页面中更改——只需更改数据库中的链接,我们的链接将无需任何麻烦地更新。

  3. 第三步是为网页准备一个小的链接示例,将其命名为:menu.asp,并编写以下代码。
    <div align="center">
    <table border="0" width="100%" height="40" cellspacing="0" cellpadding="0"> 
    <tr>
     <td width="100%" height="40" >
      <table border="0" width="100%" cellspacing="0" 
                cellpadding="0" style="border: 1px solid #949494; "> 
       <tr>
        <td align="center" valign="bottom"  bgcolor="#E2E8F5" nowrap height="21">
          
        </td>
       </tr>
       <tr>
        <td width="357%" align="center" bgcolor="#E2E8F5" nowrap height="16"  >
         <a href="teams.asp"><font size="2">The teams</font></a>
        </td>
       </tr>
       <tr>
        <td width="357%" align="center" bgcolor="#E2E8F5" nowrap height="16" >
         <a href="players.asp"><font size="2">The Players</font></a>
        </td>
       </tr>
       <tr>
        <td width="357%" align="center" bgcolor="#E2E8F5" nowrap height="16" >
         <a href="stadiums.asp"><font size="2">Our Stadiums</font></a>
        </td>
       </tr>                       
       <tr>
        <td valign="bottom" valign="bottom" bgcolor="#E2E8F5" nowrap align="right">
          
        </td>                                      
       </tr>
      </table>
     </td>
    </tr>
    </table>
    </div>

    它将如下图所示。

    Sample screenshot

  4. 最后一步是设置你的第一个页面,包含这些包含文件,并获得一个由三个部分组成的网页,你可以在许多页面中重复此操作,当你需要更新页眉或页脚中的任何内容时,只需更改一个文件,你的网站就会立即更新……创建另一个页面,将其命名为default.asp,并在其中编写以下代码。
    <div align="center">
     <center>
      <table border="0" cellpadding="0" cellspacing="0" width="761">
       <tr>
        <td colspan="2" width="761"><!--#include file="header.asp"--></td>
       </tr>
       <tr>
        <td width="63" valign="top">
          <p><!--#include file="menu.asp"--> </p>
         <p> 
        </td>
        <td width="698">
         <blockquote>
          <font face="Verdana" size="2">Welcome to our site, about the next year's 
            biggest Soccer Event in Europe, this time organized by Portugal.<br>
            Here you'll find many kind of information, such as the team's which are
            going to participate, in this event, about their players, main stars,
            such as Figo, Zidane, Raul, and many others....</font>
         </blockquote>
        </td>
       </tr>
       <tr>
        <td colspan="2" width="761"><!--#include file="bottom.asp"--></td>
       </tr>
      </table>
     </center>
    </div>

所以,我使用了不同的文件来制作一个网页,通过代码包含它们。

<!--#include file="nameoffileToinclude.asp"-->

我认为这就是你需要做的,以创建一个真正的动态网站,因为它将非常容易更新。

我写这篇文章是因为我已经创建网站两年半了,我知道每天更新网站有多么困难。你可以创建任意数量的包含文件,并将它们放在任何ASP页面上,我可以保证你的网站加载速度也会非常快,因为包含文件加载速度非常快……试试看……希望能尽快收到你的回复。

这有点棘手,因为毕竟你正在创建一个网页,到目前为止还没有发现任何ASP代码。但是当你尝试并发布你的网站时,你会意识到它的重要性,正如我所说,你可以在HTML中间放入任何脚本。

一个小小的建议:现在发布ASP页面网站非常困难,但我一直在使用Brinkster,效果非常好……只需访问它,获取一个免费帐户,并放置ASP页面(首先阅读规则……没有任何技巧,只是一些指导和需要避免的事情),数据库(首选在DB文件夹中)和各种图像,.swf文件(Flash),但在免费帐户中没有组件……

我希望这个网站的所有访问者,特别是这篇文章的读者一切顺利。我希望我已经以某种方式帮助了这个优秀网站的访问者,我认为这是一个对各种程序员都有很大帮助的社区。

对不起我的英语不好。继续努力。

注意:示例图像和脚本文件在.zip文件中,请下载……或使用你自己的图像。

© . All rights reserved.