藍色小舖

http://www.blueshop.com.tw/board/show.asp?subcde=BRD20020307142601RJ7&fumcde=FUM200410061525290EW&rplcnt=15


下面說明,因為include 為SEVER SIDE SSI語法,所以前後的if、select判斷式都不會執行

http://www.learnasp.com/freebook/asp/inc.aspx

Include Files

The include option is the heart of making efficient ASP files and re-usable chunks. It basically 
has two forms and now we will present the forms and their differences:

<!--#include virtual="/whatever.asp"-->
would include any file on your site (in this example, whatever.asp is in the web server's root directory) 
but you must fully qualify the filename with a path.

<!--#include file="whatever.asp"-->
can include the whatever.asp file in  the directory of the script that contains the statement. 
It ASSUMES the current directory!

Example #1
<!--#include virtual="/sally/filename.asp"-->
could include a file from sally's directory, even if the page with this statement is (for example) 
in the /fred/finance folder.

Example #2:
<!--#include file="/sally/filename.asp"-->
will fail from fred's directory.

Example #3:
<!--#include file="../sally/filename.asp"-->
will succed from fred's directory but if the script that contains it is moved to a different level in the tree structure 
it will fail to locate the file. INCLUDE VIRTUAL is better if a script may be moved and is immune to relative path issues.

IMPORTANT: Include files are always processed and inserted before ASP scripts on the page are calculated. Thus a page with many IFs and SELECT CASEs that selectively include files in fact always include the file before the script begins executing.


請參考下面的動態載入方式

Includes Files Dynamically

The include files are gathered and processed BEFORE any ASP code. Soif your code looks like this:

<%SELECT CASE
     CASE 1 %>
    <!--#include virtual="whatever1.asp"-->
     CASE 2 %>

    <!--#include virtual="whatever2.asp"-->
     CASE 3 %>
    <!--#include virtual="whatever3.asp"-->
<%END SELECT%>

Three includes are performed before any ASP code is executed.

YOU CANNOT DO:

<%
     whichfile="1"%>
  <!--#include virtual="whatever<%=whichfil%>.asp"-->

Though this is a reasonable idea.

<!--#include virtual="whatever.asp"-->

We however have coded a workaround that is FREE you may find useful. The workaround is:

   filename=/learn/test/includedynamic.asp

<Test Script Below>

<html><head>


<TITLE>includedynamic.asp</TITLE>


</head><body bgcolor="#FFFFFF">


<%


whichfile="bookscifi.asp"


Call ReadDisplayFile(whichfile)


response.write "<hr>"





whichfile="bookhorror.asp"


Call ReadDisplayFile(whichfile)


response.write "<hr>"








whichfile="/learn/test/bookmarketing.asp"


Call ReadDisplayFile(whichfile)


response.write "<hr>"


%>





</body></html>


<%


SUB ReadDisplayFile(FileToRead)


whichfile=server.mappath(FileToRead)


Set fs = CreateObject("Scripting.FileSystemObject")


Set thisfile = fs.OpenTextFile(whichfile, 1, False)


tempSTR=thisfile.readall


response.write tempSTR


thisfile.Close


set thisfile=nothing


set fs=nothing


END SUB


%>


 

 

The only downside to this method is no ASP Code ( i.e. anything in <% %> ) will be parsed or executed in the included file. If you must execute ASP code the Win2k server.execute @
/freebook/asp/incwin2k.aspx
or the 3rd party components like ASPHTTP must be employed @
/learn/include/asphttp.asp



Includes - Other Sites/Dynamic FileNames

3rd party components like ASPHTTP can grab the HTML contents of a specific URL into an ASP string. Supports GET/POST/HEAD documents via the HTTP protocol, examining response headers, transfering requests to a file (including binary transfers) and password authentication support. They can also be used on YOUR OWN SITE to make dynamic includes possible.

available at http://www.serverobjects.com/products.htm

Microsoft includes WinInet which seems to be ideally suited for this EXCEPT is currently not thread safe, see:
http://www.learnasp.com/advice/threadsafe.asp
so Tools like ASPHTTP are a necessity.

Here is a sample where ASPHTTP is used to grab contents from another site.

   filename=/learn/test/asphttpother.asp

<Test Script Below>

<html><head>


<title>asphttpother.asp</title>


</head>


<body>


<%


Set HttpObj = Server.CreateObject("AspHTTP.Conn")





HttpObj.Url = "http://www.funinspace.com"


strResult = HttpObj.GetURL





STRresult=server.htmlencode(STRresult)


response.write STRresult





SET HTTPobj = nothing


%>


</body>


</html>








 

 

Here is a sample where ASPHTTP is used to allow a string to decide  which page on our site is executed.

   filename=/learn/test/asphttpdynamic.asp

<Test Script Below>

<html><head>


<title>asphttpdynamic.asp</title>


</head>


<body bgcolor="#FFFFFF">


<%


mystring="/learn/test/response.asp"





Set HttpObj = Server.CreateObject("AspHTTP.Conn")


HttpObj.Url = "http://www.learnasp.com" & mystring


strResult = HttpObj.GetURL


response.write STRresult





SET HTTPobj = nothing


%>


</body>


</html>



或是下面網址的方式









http://www.asp101.com/articles/michael/dynamicincludes/default.asp

Dynamic Include Files

by Michael Qualls

Introduction

This is an advanced example and it assumes that you are already familiar with HTML, ASP,
and using ActiveX Objects. In it I use the FileSystemObject and TextStream Objects to create
"dynamic" includes.

Dynamic Include Files

I think that at one time or another all of us have attempted to create dynamic include files
in our ASP applications only to find out that it would not work. The usual approach was to
use a variable to hold the name of the file that you wanted to include and then pass the
name of that variable to the include directive.

Let us be clear: The following code will not work:

<%


'A variable is declared to hold a file name


Dim MyFile





'The desired file name is passed to the variable.


MyFile = Request("SomeFileName")





'Then the variable holding the file name is passed


'to the include directive.


%>


<!--#include file=<%=MyFile%>-->


The reason that this code listing will not work is because in Active Server Pages the include
directives are resolved before the server-side script portions are processed.
The above code listing will trigger a nasty error telling you that the include file cannot be found.

One of the reasons to use an include file is to be a container for static HTML
that does not normally change (such as a standard header or footer).
This file is then referenced using a normal include directive. However, sometimes,
different content is needed based upon user input or some other condition.
Rather than create an entirely different web page for each and every possible condition,
the idea of using "dynamic include" files was born. However, as noted in the code listing above,
the most logical way to implement this idea does not work.

In order to get around this problem, the Microsoft FileSystemObject Object can be used to load
and pass the content of the desired include file into a string variable which can be inserted into
the page that is being sent to the client. The following function, "getFileContents",
aids in this process by loading a file that is passed to the function as an input parameter and
returning its contents as a string.

<%





'Pass the name of the file to the function.


Function getFileContents(strIncludeFile)


Dim objFSO


Dim objText


Dim strPage








'Instantiate the FileSystemObject Object.


Set objFSO = Server.CreateObject("Scripting.FileSystemObject")








'Open the file and pass it to a TextStream Object (objText). The


'"MapPath" function of the Server Object is used to get the


'physical path for the file.


Set objText = objFSO.OpenTextFile(Server.MapPath(strIncludeFile))








'Read and return the contents of the file as a string.


getFileContents = objText.ReadAll





objText.Close


Set objText = Nothing


Set objFSO = Nothing


End Function


%>


Using this function "dynamic include" files can be achieved. First, the main page
(a template file containing the page layout and any static content) would be loaded and passed
to a string variable. Then, the contents of the include file would be loaded and passed to a string variable.
Finally, the variable containing the contents of the include file would be inserted into
the contents of the variable containing the main page.

Example: Dynamic Include Files

First, lets take a look at the "template" file. In this listing, there is an HTML comment,
"<!-- INCLUDE FILE HERE -->". We will replace this HTML comment with the content of the include file.

<html>


<body>


<h2>Welcome to my web page!</h2>


<table width="500" border="1">


<tr>


<td>


<!-- INCLUDE FILE HERE -->


</td>


</tr>


</table>


</body>


</html>


Now, lets look at the include files that will be used for this example. The first include file is the
default include file. The default include file is a form that allows the client to choose which of three
include files to load. Notice that the "action" attribute of the form is omitted. This is because when
the form will be submitting to itself (causing "dynamicinc3.asp" to reload).

<!-- BEGIN DEFAULT INCLUDE -->


<form method="post">


<h3>Select the name of the file you wish to load</h3>


<p>


<select id=cboFile name=cboFile>


<option value="includefile1.inc">File #1</option>


<option value="includefile2.inc">File #2</option>


<option value="includefile3.inc">File #3</option>


</select>


<input type="submit" value="Submit">


</p>


</form>


<!-- END DEFAULT INCLUDE -->


For the sake of this example, the content of the other three include files has been kept very simple.

<!-- BEGIN INCLUDE FILE #1 -->


<h2 style="color:red">FILE #1 CONTENTS</h2>


<br>


<a href="dynamicinc3.asp">Return to default page</a>


<!-- END INCLUDE FILE #1 -->


<!-- BEGIN INCLUDE FILE #2 -->


<h2 style="color:green">FILE #2 CONTENTS</h2>


<br>


<a href="dynamicinc3.asp">Return to default page</a>


<!-- END INCLUDE FILE #2 -->


<!-- BEGIN INCLUDE FILE #3 -->


<h2 style="color:blue">FILE #3 CONTENTS</h2>


<br>


<a href="dynamicinc3.asp">Return to default page</a>


<!-- END INCLUDE FILE #3 -->


Finally, consider the ASP file that makes the whole example run, "dynamicinc3.asp".

<%





'-------------------------------------------------------------


'The "getFileContents" function should be included at the top


'of the ASP file.


'-------------------------------------------------------------





'Declare variables to hold the content of the main page and


'the include file.


Dim strMain, strInclude





'Get the contents of the main page and pass them to the "strMain"


'variable.


strMain = getFileContents("maintemplate.inc")





'Test to see if the "cboFile" select box is being submitted. If so,


'load the requested include file. If not, load the default include.


If Request.form("cboFile") = "" Then


strInclude = getFileContents("includedefault.inc")


Else


strInclude = getFileContents(Request.form("cboFile"))


End If





'After the proper include file contents are loaded ("strInclude"),


'then insert it into the main page ("strMain") using the "Replace"


'function.


strMain = replace(strMain,"<!-- INCLUDE FILE HERE -->",strInclude)





'Use the "Response" Object to "Write" the completed page to the client.


Response.Write strMain





%>


This sample works, and, in effect, creates the ability to use dynamic include files because it does
not use the include directive. Instead this example uses the FileSystemObject Object.

This technique can be useful if you wish to keep your layout separated from your content.
You can create a template which contains the layout for your website and include files which
contain the content of the website. Then using ASP, you can combine the two for client viewing.
This is exactly the way that I put my website together!

Let me give you one last thing to think about. How would using techniques similar to this aid you
if you are storing your data in XML files?

Related Links


arrow
arrow
    全站熱搜

    bgm 發表在 痞客邦 留言(0) 人氣()