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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Conditional 'include' files - Can I do it? 3

Status
Not open for further replies.

LEICJDN1

Technical User
Nov 27, 2002
201
GB
Hi all.

I have an ASP page, and wish to modify the appearance of a table depending on a variable.

Both tables are quite different in layout, so I though the easiest way would be to have two different include files, one for each table. Then, after I call the variable from the database, I could choose to inlcude the relevant file.

But can that be done? I know the include has to be in the html part and not enclosed in ASP tags.

Basically how can I do this:

If type="1" then include table1 else include table2

(not code, just the principle)

I could populate each cell of the table with ASP code to display one thing if type 1 and another if type 2 but that will be cumbersome to maintain, so I thought having two different tables would be a neat solution.

If I cannot do it with includes, how can I have two different tables and choose which one to display?

Thanks.
 
It can be done. Here's a snippet from a page where I did something similar:
Code:
<td width=&quot;75%&quot; valign=&quot;top&quot;><hr>
	<%Select CASE myPage
	Case &quot;Start&quot;%>
	<!--#Include file=&quot;Start.asp&quot; -->
	<%CASE &quot;Register&quot;%>
	<!--#Include file=&quot;Register.asp&quot; -->
	<%CASE &quot;Order&quot;%>
	<!--#Include file=&quot;Order.asp&quot; -->
	<%CASE &quot;EditAccount&quot;%>
	<!--#Include file=&quot;EditAccount.asp&quot; -->
	<%CASE &quot;ForgotPw&quot;%>
	<!--#Include file=&quot;ForgotPw.asp&quot; -->
	<%CASE &quot;OpenOrder&quot;%>
	<!--#Include file=&quot;OpenOrder.asp&quot; -->
	<%CASE &quot;Checkout&quot;%>
	<!--#Include file=&quot;Checkout.asp&quot; -->
	<%END SELECT%> 
	</td>
 
no to the conditional includes

however you can do conditional executes and that is the means in which I use when this is a task to perform

eg:
If this condtion = condition
Server.Execute &quot;file.inc&quot;
else
Server.Execute &quot;other.inc&quot;
End if



_____________________________________________________________________
Where have all my friends gone to????
onpnt2.gif

 
hm. That worked Veep? I tried a few mod's once to doing this and I was unable to get it to work correctly. Off to try it out

_____________________________________________________________________
Where have all my friends gone to????
onpnt2.gif

 
Yeah, it works fine. Actually it's part of a mission critical app so if it didn't I'd be in big trouble.
 
[smile] I'm glad to get my TT time back and work with you all again.

I never thought I would miss going to a site before

now we need to get Tarwn to get his free time back [lol]

_____________________________________________________________________
Where have all my friends gone to????
onpnt2.gif

 
Thanks!

Not used executes before. Veep's method seems to work fine. Am I right in thinking there may be an issue with server overheads using Veep's method, as I assume the 'first pass' will load all the include files, then the ASP code will determine which one to load. I guess if the include files were large this may have an impact on time and server traffic.

Are executes a better solution then? What are the benefits / drawbacks of server executes?

Thanks again.
 
Tarwn jumps in every once and a while with one of his awsome theoretical dissertations, but not often enough.
 
I think the executes are better for a large information task. The includes will be loaded into memory thus taking processing time while the executes will not. Althoguh both will have there personal limitations and qualities on each other. I recommend trying both and testing the performance of each to better evaluate your particular needs.

_____________________________________________________________________
Where have all my friends gone to????
onpnt2.gif

 
The way I look at it, the server doesn't see the include directive unless the SELECT CASE statement allows it to so only the the file that is called for gets loaded. Of course, I've been wrong before ;-)
 
I think my downfalls in my attempts was the If conditional. I had never tried a case selection and that may be the differential here. I tried the case and about smacked myself for never trying it prior as it worked good. Thanks for the tip Veep.

I'm off to try the load expectations of each of the techniques. possibly post the results later today

_____________________________________________________________________
Where have all my friends gone to????
onpnt2.gif

 
I knwo why my thoghts on the topic are getting a bit frayed. thinking condtitionaly on includes in a sense of conditional names or value concates to them.
eg:
can't do
<!--#include file='&quot;& path &&quot;'-->
seeing as the order of operations to load on the ASP/Include side of things

just a small addition to the thread.

_____________________________________________________________________
Where have all my friends gone to????
onpnt2.gif

 
The thing about INCLUDE's is: the content of them is INCLUDED, whether you want it EXECUTED or not... so:

Code:
<%if 1 = 1 then%>
<!--#INCLUDE VIRTUAL=&quot;/inc/file1.htm&quot;-->
<%else%>
<!--#INCLUDE VIRTUAL=&quot;/inc/file2.htm&quot;-->
<%end if%>

...actually reads through the entire content of file1 AND file2, even tho only one of the files' content is presented... which sucks.

The Server.Execute() directive only reads the &quot;target&quot; file IF it's actually the one executed.

It also accepts variable-values, so:

Code:
<%select case iVar
    case 1
      exFile = &quot;/inc/file1.htm&quot;
    case 2
      exFile = &quot;/inc/file2.htm&quot;
end select

Server.Execute(exFile)
%>

...works just dandy.

[red]Problem[/red] When you use INCLUDES, you can imbed your main-page's variables withing the INCLUDE file... not so with EXECUTED content. Variables in the Main-page are NOT available within the the EXECUTING code... nor are variables created during EXECUTION available back in the Main-page.

You do, however, still have access to the Server and Request variables, so you can use Request.QueryString(&quot;varname&quot;) in your EXECUTED content.

I use EXECUTES whereever possible, rather than INCLUDES... but still use INCLUDES for things like Connections and Error-handlers.
 
That's what I love about this stuff. Ya' learn something every day. :)
 
Thanks all. Very helpful and interesting. My concerns regarding the 'inefficiency' of INCLUDES seem to be correct, so I will experiment.

Interesting point about the different variable handling between the two methods.

Thanks again.
 
Good article onpnt :
Obviously you should avoid large include files with a lot of code that only gets executed in some pages that include the file.

:( I always thought an include file to be perfect for holding all the functions you may or may not use in your pages.

Can you use Server.Execute() to call a function? Can you pass in parameters and get a value returned?

If not I guess that is something seriously lacking in ASP. (presume it is sorted out in asp.net, and afaik PHP can do this already)

Posting code? Wrap it with code tags: [ignore]
Code:
[/ignore][code]CodeHere
[ignore][/code][/ignore].
 
Hi clarkin
Can you use Server.Execute() to call a function? Can you pass in parameters and get a value returned?


Yes and No.
Life would be great if you could but the only way to get it to work with this functionality
is to use session objects as the parameters. Obviously that can get resource hungry real fast
seeing as includes and server.execute themselves take a lot of resources due to caching issues.

I'm kind of excited because I think this is a great topic to discuss and something of a
discussion it seems we've gone away from lately,. (in discussing anything really)

So to stack this up on the sessions provided our resources are not dragged down the drain
and we're starring at a blank screen for a bit [lol] this is a great way to actually perform
minor to moderate tasks that will make 1) the pages (scripts) more structured and 2) far easier
for later maintenance and debugging.

let's say we make a page named exampleExecuteOne.asp
this page will have nothing but a form and a If statement to conditionally call the execute
Now here's the gotcha! You don't really want to use this method out of a conditional statement.
at least I've not liked to in the past. It makes error handling a bit cumbersome and you
may cause issues with the view of the outputted page as a value can effectively be sent empty
to your page. Although as everything a few conditional expressions can get you out of things
of that matter. anyhow, onto the first page

short and to the point for our example
steps in sequential line
1) client function to set submission
2) conditional check if submission occurred
3) load form if not
4) set session values
5) execute the function in second page
6) all done

<html>
<head>
<script language=&quot;javascript&quot;>
function subFrm() {
frm.submitted.value = &quot;subbed&quot;;
return true;
}
</script>
</head>
<body>
<%
If Request.Form(&quot;submitted&quot;) = &quot;&quot; Then
%>

<form name=&quot;frm&quot; method=&quot;POST&quot; onSubmit=&quot;return subFrm()&quot;>
<input type=&quot;hidden&quot; name=&quot;submitted&quot;>
<input type=&quot;text&quot; name=&quot;myName&quot;>
<input type=&quot;submit&quot; value=&quot;Sub It!&quot;>
</form>

<%
Else
Dim TheName : TheName = Request.Form(&quot;myName&quot;)
Session(&quot;TheName&quot;) = TheName

Server.Execute(&quot;myFunctions.asp&quot;)

End If
%>
</body>
</html>

obviously looking at this the only thing I think we get out of it is structure to
the page and ease of reading for everyone that may follow. That is a major part
of structured programming though and I am a strong believer in doing things like this
to make it easier for the next programmer.

so the second page being our function we do a simple
name: myFunctions.asp (you can use a .inc I believe also)
<%
Function playWithValues(TheName)

If Len(TheName) <= 0 Then
Response.Write &quot;Sorry, the was a error in the passing of your name<br>&quot;
Else
Response.Write &quot;Good morning <strong>&quot; & TheName & &quot;</strong>&quot;
End If

End Function

playWithValues(Session(&quot;TheName&quot;))
%>

now in this case we'll always output something, but to make this really fucntional and
useful a check for empty, isNull(), Len etc. in the session var could be done to exit the
function prior to any calc's or output. That would be obviously the proper and most likely
use for this method. something like

wow! that was a bunch of blabbing. [smile]
<%
Function playWithValues(TheName)

If Len(TheName) <= 0 Then
Response.Write &quot;Sorry, the was a error in the passing of your name<br>&quot;
Else
Response.Write &quot;Good morning <strong>&quot; & TheName & &quot;</strong>&quot;
End If

End Function

If session(&quot;TheName&quot;) <> &quot;&quot; Then
playWithValues(Session(&quot;TheName&quot;))
End If

%>
hope that helps out in the future when you think of something you need like this.

onpnt


_____________________________________________________________________
onpnt2.gif

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top