PDA

View Full Version : How to pass a variable to an included file?


Grant Palin
04-28-2004, 01:37 AM
I'm setting up and ASP page that includes header and footer files, header.asp and footer.asp, respectively.

I also have an index page, index.asp, that includes the first two files. Index.asp also contains some content, and so the index page should have a particular title.

I can include the first two files intot he third without a problem, but I try to create a page title variable int he index file, and display it in the header file inside the HTML title tag. Problem is, it doesn't work.

In index.asp:

<%
Dim PageTitle
PageTitle = "Population and Demographics"
%>
<!-- #include file="header.asp" -->


In header.asp:

<html>
<head>
<title><%= PageTitle %></title>
...


I know you can do this in PHP - I've done it. But it doesn't seem to work in ASP. Any suggestions?

glenngv
04-28-2004, 04:51 AM
Include files are process first before the codes. Try Server.Execute() instead.

<%
Dim PageTitle
PageTitle = "Population and Demographics"
Server.Execute("header.asp")
%>

Roy Sinclair
04-28-2004, 04:22 PM
I've done that in ASP without any problems, include files are processed when they are reached within the source so anything in your source before the include should be usable from the include.

You say it isn't working but not what's not working. Are you sure you haven't put the <html>, <head> or <title> tags into the primary file first? (That would make the "included" one not work).

Grant Palin
04-28-2004, 06:14 PM
#$*&&)(*(*%#$R&^$%&!
I realized the problem: I was including the page BEFORE I declared the title variable! :mad: (despite what I type in my original post!)

I moved the include to after the variable declaration, and the title appears properly now.

Yes, I have the html, head, title... tags in the header file - nowhere else.

raf
04-28-2004, 08:17 PM
I've done that in ASP without any problems, include files are processed when they are reached within the source so anything in your source before the include should be usable from the include.
I think the comfusion is that the incudes are indeed included before the parsing, but they are then processed like any other code, when the parser reaches it.