Riquelmy Melara
Riquelmy Melara

Reputation: 941

Pass Variable from Content page to Master Page in classic ASP

I am new to classic ASP and I am trying to create a Master Page with variable placeholders and fill the information on that page with variables that are worked on a content page.

my master page looks like this:

<html>
<head>
  <title>Template Loaded Properly</title>
</head>
<body>
  <% call BodyContent %>

  <span>Title: <% //place title here %></span>
  <span>Content: <% //place content here %></span>
</body>
</html>

and the content page like this:

<!--#include virtual="/templates/TEMPLATE.html" -->

<% sub BodyContent %>
var Title = "This is the title"
var Content = "Here goes some content"
<% end sub %>

Any help will be appreciated.

Upvotes: 1

Views: 228

Answers (1)

Martha
Martha

Reputation: 3854

Once you include the page with the variables, you can treat them as if they were created right then and there (because in a sense they are created right then and there, at least from the server's point of view). You do need to make the variables global in scope [read: dim them outside the sub], unless you want to list all of them when calling your BodyContent sub. (Personally, I don't see the point, but some people are unreasonably allergic to global variables.)

<% 
dim Title, Content
sub BodyContent
    Title = "This is the title"
    Content = "Here goes some content"
end sub 
%>
<body>
   <% call BodyContent %>

   <span>Title: <%=Title%></span>
   <span>Content: <%=Content%></span>
</body>

One caveat, though: include files are processed long before the code, so you can't vary what file is included. In other words, don't do this:

<%If x = a Then%>
<!-- #include virtual="/templateA.inc" -->
<%Else%>
<!-- #include virtual="/templateB.inc" -->
<%End If%>

The result of trying something like that is that both templateA and templateB will be included. If you need conditional includes, look into using FileSystemObject to read the content of the appropriate template, and then using Execute to, well, execute it.

Upvotes: 2

Related Questions