Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Is it possible to use #include virtual with IF?

Status
Not open for further replies.

webgs

Programmer
Oct 30, 2001
59
US
I need to test a condition and then load an include file based on the condition. Like this:

<%
if pagenum=601 then
<!---#include virtual='/file.txt'--->
end if
%>

I've tried many variations and I can't get it to work.

Any suggestions? Is it even possible?

 
includes are processed before inline asp code.

So your include is processed before the if.

If you are using IIS 5 or 6, try using server.execute instead.

- Spicolli
 
I don't believe this is possible. Include files get processed before ASP so it can't work that way... you could put the if statement inside the include file though.
 
I'll go the AdamOK route. Thanks JSpicolli!
 
Actually, that will work just fine...
<%if 1 = 2 then%>
<!--#INCLUDE VIRTUAL="/inc/fileA.asp"-->
<%else%>
<!--#INCLUDE VIRTUAL="/inc/fileB.asp"-->
<%end if%>

The actions/results/output of the selected INCLUDE will appear on your web page.

The problem is, YES -- BOTH files get executed prior to one of them being displayed. So, it's a bit inefficient.

But, if you go the (admittedly preferred) Server.Execute("/fileB.asp") route, you may run into some other trouble. Here's why:

When you create variables on your page, the INCLUDE files have full access to them... so, the code inside your INCLUDE can use those variables to alter their results.

Files which are EXECUTED cannot access page-variables... nor can they pass page-variables back to be used by the main page AFTER the code is executed. Executes are entirely self-contained. They DO, however, have access to the Server variables, and the Request object... so you CAN have things like <%if request.querystring("varName") = "bob" then%> in your execute file.

The best thing about executes (IMHO) is that the code is ONLY executed IF it is the selected code. Also, the execute command accepts a variable name, so the following would work just fine, too:
<%
select case varName
case 1
incFile = "/inc/case1.inc"
case 2
incFile = "/inc/errormsg.inc"
case 3
incFile = "/people/webmaster_bio.htm"
case else
incFile = "default.inc"
end select

Server.Execute(incFile)
%>
 
I agree with Mr3Put, although...

Execute is not meant to be a replacement for a traditional include.

IMHO, this is exactly the type of scenario that server.execute was designed to handle.

It also helps design a logically structured application.

Having IIS process all your include files when they are not needed is a very poor design and you will most definately get a significant performance boost by replacing your "conditional" includes, with server.execute logic.

Includes were never designed to be used in conditional processing. They were designed for encapsulating some reusable chunk of code.

I think Mr3putt has provided you a great demonstration of how to use conditional includes efficiently.
 
Oh, and BTW, webgs... the reason your "conditional" includes were not working is:

<%
if pagenum=601 then[red]%>[/red]
<!---#include virtual='/file.txt'--->
[red]<%[/red]end if
%>

The INCLUDE statement cannot be encapsulated inside your scripting tags... add the [red]red bits[/red], and your conditionals should work.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top