views:

316

answers:

2

I have to use classic ASP to build a website. I want to be able to use include files for the header and footer.

How do I pass variables to the include files so that I can affect such things as titles etc. Here is some examples of what I want to do:

index.asp

<%
dim title
title="TITLE"

'Server.Execute("header.inc") <--- Note I tried this but it didnt work also
%>
<!--#include file="header.inc" -->

header.inc

<html>
<head>
    <title><% Response.write title %></title>
+5  A: 

document.write is client-side JavaScript. What you want in header.inc is:

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

Or more verbosely:

<html>
<head>
    <title><% Response.Write title %></title>

Other than that, what you have should work (without the Server.Execute, just do the include as you show further down).

T.J. Crowder
Cheers I spotted that mistake but it still doesn't work, I think I have to use Session
Chris
@Chris: This works just fine, you don't need to use Session for it. I just tested it to be sure.
T.J. Crowder
@Chris, What T.J. Crowder has posted is working 100%. You will only find obstacles, with this method, if you want to dynamically include files..
Gaby
A: 

As far as I can tell using sessions is the only way

<%
Session("title") = "mytitle"

Server.Execute("header.inc")
%>

<title><% response.write(Session("title")) %></title>

This article also shows the same results

Chris
No, you don't need to use sessions. I just tested my answer and it works just fine.
T.J. Crowder
Use <!--#include file="header.inc" --> (as in your original example) to include the header file and then you can simply reference the title variable as @TJCrowder shows. When you include a file, ASP imports the contents of the included file before processing any of the code on the page. Any page-scope variables defined in the ASP page will also be available in the include file (and vice versa).
Simon Forrest