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

Includes, Dynamic Statements and Variables 2

Status
Not open for further replies.

radiance

Programmer
Jan 4, 2003
164
0
0
US
I have a simple website that I am putting together as an asp newbie. Typically, I use Coldfusion, in which variables can be set in the header and application page and called dynamically.

Ideally I would like my pages to display based on the conditions set in the header and other files. I think that I need to use conditional statements for my header and footer includes and well as my navigation. I am having trouble though calling the variables set on their respective pages.

For example, I am testing one page which I have name examples.asp. It should have the included header and footer files. The navigation also has conditions and javascript. I also want to have dynamic title displays. However, the file is not showing the header template. In coldfusion, I can set the header variables in my header files, and define the navigational elements in their
Can I set the variables equal to a value and then set then on their respective pages? Is this possible in ASP.

----Page for Header- Conditional Statement-----
Here's my basic statement for my header.

<%@ Language=VBScript %>
<%Option explicit%>

<%
Dim TitleStr
%>

<html>
<head>
<title><%=TitleStr%></title>
<link rel="STYLESHEET" type="text/css" href="/global/sf.css">

</head>

<body topmargin="2">

<%
DIM main

If main=1 THEN

%>
Header Content---Tables with Images for Day

<%
Elseif main=2 THEN
%>
Header Content---Tables with Images for Day
<%
End If
%>

End of Header File....

EXAMPLES.asp Page:
I am trying to call my header and footer based on the variables set.

<!--#include virtual="global/head.asp"-->
<%
TitleStr = "Southfive Examples"
%>

<%
main = 2
%>

CONTENT for Examples Page


<!--#include virtual="global/foot.asp"-->
 
You should be getting an error. Option Explicit can only be used on the first line of the file. The way ASP works is that any file you include it basically pastes into the calling code and then it goes through and executes the whole long thing.

So by including the Language and Option Explicit in the include file you shsould be receiving an error, these are only applicable to the top level page (in this case, Examples.asp).

Second problem, your not defining a value for the TitleStr variable until after you have already used it. Here is the order your code is executing in (based on your includes):
Code:
<%@ Language=VBScript %>
<%Option explicit%>

<%
Dim TitleStr
%>

<html>
<head>
<title><%=TitleStr%></title>
<link rel="STYLESHEET" type="text/css" href="/global/sf.css">

</head>

<body topmargin="2">

<%
DIM main

If main=1 THEN
 
%>
Header Content---Tables with Images for Day

<%
Elseif main=2 THEN
%>
Header Content---Tables with Images for Day
<%
End If
%>
<%
TitleStr = "Southfive Examples"
%>

<%
main = 2
%>

CONTENT for Examples Page


<!--#include virtual="global/foot.asp"-->

Right now your getting away with the option explicit and language in the secondary file because you don't do anything before including that file, but you put anything befoe that include and it will throw an error (surprised it isn't anyways, but anywho).

Now, As you can see, the code inside your include file is called before you ever set the TitleStr variable or your main variable. The code executes from top to bottom, and the variables your outputting in the include file (top) aren't even given values until the middle. I sokmehow doubt that CFM wouold have let you prinbt out a value before you set it either, this just may be structured differantly then you used to.

So first things first, you need to either define the TitleStr variable in your Examples.asp page and set it before you do the include, or better, you could encapsulate the entire footer display in a function and pass the titlestr as a variable, like so:
Code:
<%
Function ShowHeader(titleStr,mainFlag)
   %>
   <html>
   <head>
   <title><%=titleStr%></title>
   <link rel="STYLESHEET" type="text/css" href="/global/sf.css">
   </head>
   <body topmargin="2">
   <%
   If mainFlag=1 THEN
      %>
      Header Content---Tables with Images for Day
      <%
   Elseif mainFlag=2 THEN
      %>
      Header Content---Tables with Images for Day
      <%
   Else
      Response.Write "Invalid value [" & mainFlag & " for Main!"
   End If
   %>
End Function

Now you can still include this file at the top of thepage, but when you want to show the header you could just call it like so:
Code:
ShowHeader "This is my title with main 1",1

or

ShowHeader "This is my title with main 2",2

I generally place both my header and footer in the same include file and just include that file at the top. Then I just call my header function where I need it and call my footer function later when I need it.

Hope this helped,
-T

barcode_1.gif
 
If I want to show the header based on the value of 2 on the examples.asp page, do I need to put <%%> around ShowHeader "This is my title with main 2",2?? I am having problems with the proper syntax.

Thanks so much.
 
Yes, since those are ASP function calls they need to be in a code block surrounded by <%%>
The brackets don't need to be directly around them, just inside a block with those - the reasoin I clarify is that I once knew a guy that started on CFM who used to write code with the brackets on every line:
Code:
<% Dim a %>
<% a = "Hello World" %>
<% Response.Write a %>

That is bad practice because it is needlessly switching between ASP and HTML, kinda like shifting your car into neutral then back into gear everytime you increase your speed by 2 mph, there is no point and your just wearing down the system [car] and wasting time (in ASP terms, your chewing up resources and adding time to the execution time, neelessly).

Anyways, you may not have needed the mini-lecture, just remember tryiong to decode some guys code several years back :p

For reference, a better way to write the previous block of code would have been:
Code:
<%
Dim a
a = "Hello World"
Response.Write a
%>

-T

barcode_1.gif
 
Thank You for your help. Do the same concepts apply for conditionals within navigation?? I will try your suggestions. I may have a few more questions (just so that I don't create a mess). Do you also have suggestions for resources? I have read the 3guysfromrolla.com and a few other asp tutorials, but many of the tutorials seem to leave out proper syntax.

 
My page is still not coming out. The header code is different depending on the variable. On the sample examples.asp page, I want to call header 2 and write any generic title. This is what I have for the asp code on examples.asp:

<!--#include virtual="global/head.asp"-->

<% ShowHeader "Examples of Our Work",2
%>
Content for Examples Page
<!--#include virtual="global/foot.asp"-->


Should the function go above the include??? Is there another way syntatically to call the function so that My header will show?


Which just gives me an error, no page.

What am I doing wrong? Do I need to redefine the function?

Please help...

Thanks.

 
I also tried this to, but now, I don't get the main portion of the head file, only the content on the examples page and the footer (which I have not set conditions for yet).

<!--#include virtual="global/head.asp"-->

<% ShowHeader = "Examples of Our Work,2"
End Function
%>

Content
<!--#include virtual="global/footer.asp"-->


 
Here's how the page is processed, in a nutshell.

1. Any include is replaced with the actual code from the include, just as if you'd copied it from the referenced page and pasted it in place of the include.

2. The now-complete page is processed for syntax, looking for errors. If a function is referenced, it looks on this now-complete page for that function.

3. The now-complete page code is executed, top to bottom (obviously following control statements). Function declarations are only run if they're called.

With that basic framework, you should be able to trace through your page and see what the ASP processor sees.

Sorry if that sounds too simplistic -- I just want to make sure you understand what's going on overall.
 
Your explanation on ASP structure has made this process for me less of a headache (or a migraine), however, I think that I am not calling the function correctly on the examples.asp page, based on the value of the page.

Examples should show header 2.

Examples.asp should have:

'the include here

'the function here

In this case I am calling the function showHeader (defined on the head.asp page as
<%
Function ShowHeader(titleStr, main)
%>

Now calling the above function on the examples page as
<%=ShowHeader(Examples of Our Work,2)%>

It's not showing, so I must be doing something incorrect with the syntax.

Please advise.
 
Okay. I have been trying to work through this and am now only getting Expected 'End' error. Can't figure out where i need to add another 'End'. I still think it's my syntax, perhaps I am missing a ')'???

I now have this on my head.asp page (which is included)
<%@ Language=VBScript %>
<%Option explicit%>
<%
Function ShowHeader(titleStr, main)
%>

<html>
<head>
<title><%=titleStr%></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<javascript code for navigation>
</script>
</script>

<link rel="STYLESHEET" type="text/css" href="/global/sf.css">

</head>

<body topmargin="2">

<%
If main=1 THEN
%>
Blue Content for Header of file

<%
Elseif main=2 THEN
%>
Red Content for Header of File

<%
Else
Response.Write "Invalid value [" & main & " for Main!"
End If
%>
End Function

========================================
This is on my examples.asp page

<!--#include virtual="global/head.asp"-->
<%ShowHeader "Testing",1%>
Dummy Content for Example Page
<!--#include virtual="global/foot.asp"-->
 
Sorry, my monitor went dead last night so i haven't been on (on a 17" with hello kitty stickers all around it now...this is going to give me issues)...


Ok, 1)
You should move the Option Explicit and Language calls to the main file (Example.asp), they should never be in the include files. Your only getting away with it right now because you include the file without doing anything before it, if you add any lines of code before your include then it's going to spit out an error about Option Explicit not being on the top.

2) Everything inside <% .... %> is ASP, everything outside is plain text/html. So in your include file you need to move the End Function into the ASP block, otherwise it is attempting to send it to the client browsr as html (which is why your getting the missing end error, it doesn't see an End for that function anywhere)

3) The proper way to call that function was the on you used originally: ShowHeader "Some String",#
It will likely give you an error if you try to put quotes around the string and # because you will not be supplying enough arguments to the function (Expected 2 Arguments, Received 1 Error)


Note: Now you could also write a ShowFooter function and put it in the same page that has the header, that way youwould only need the one include at the top then when you want the footer you could call the ShowFooter function (at the bottom). This would cut down on the number of includes that needed to be tracked down. On the other side it would increase the size of the include file but since you planned on using both functions it wouldn't be a performance issue as you would have needed that much content anyways.


- I apologize if I rambled a bunch, hard to keep track of the thread of my post when the window is so tiny...aparently this monitor doesn't like 1280x1024, my normal resolution

-T

barcode_1.gif
 
Thank You Tarwn. I will try modifying these changes this evening and keep you posted. Thanks for your patience with an asp newbie.
 
Thank You Tarwn and Genimuse for your patience in explaining ASP syntax!!!! Because of your explanations, my code is working fine! It will take a bit more practice, but I will get it.

THANK YOU.

I don't know how to give both of you stars, but a note to the moderator, you both deserve the highest!!! :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top