Classic ASP Master Pages

By · March 27, 2010 · 6 comments

When developing websites most of them have common elements like headers, footers and navigational menus that are common throughout the site. In Classic ASP we mostly use multiple server side includes to accomplish this. I like ASP.NET master pages and this page details the technique I use to achieve something similar in Classic ASP.

My technique involves creating a master page ASP file which has all the common elements. Where ever I want content to be added I add a call to a sub routine.

Then in my content pages when I want the master page content to be added I include it at that point. I then create a sub routine for each content area and put my content inside these. You can have as many content sub routines as required but they must exist in every content page that uses that master page.

The following example shows a master page and content page with two content areas: one inside the head tag (called HeadPlaceHolder in this example) so I can easily add title and meta tags, and add JavaScript and CSS within the head tag if required; and one inside the body tag between the header and footer (called ContentPlaceHolder in this example).

Any ASP variables that are needed by more than one content area should be created outside of the sub routines.

masperpage.asp

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<% Call HeadPlaceHolder() %>
<link rel="stylesheet" type="text/css" href="stylesheet.css" />
<script language="javascript" type="text/javascript" src="/java.js"></script>
</head>
<body>
<div id="header">
Logo etc etc
</div>
<div id="content">
<% Call ContentPlaceHolder() %>
</div>
<div id="footer">
Copyright etc etc
</div>
</body>
</html>

content-page.asp


<!--#include virtual="masterpage.asp"-->

<%
'Any ASP variables that are needed by multiple contenbt areas should be declared
'outside of the sub routines
'It's a good practice to keep your ASP script at the beginning of your pages anyway
%>
<% Sub HeadPlaceHolder() %>

<title>My page title</title>
<meta content="My meta description" name="description" />

<% End Sub %>

<% Sub ContentPlaceHolder() %>

<p>My page content goes here</p>

<% End Sub %>
Read more...
Classic ASP

Get the latest posts delivered to your inbox.

Comments (6)

# Posted by Pere · January 26, 2011

It doesn't work if you want to set dinamic content.
Ex:
<% Titl = "My dinamic content, form BBDD"
Sub HeadPlaceHolder() %>

<title><%=Titl%></title>
<meta content="My meta description" name="description" />

<% End Sub %>

But is a great code.
I'm fighting to find a way to change dinamically my page's Title if you have an include, that contains <head></head>, befor your content page.

Reply

Pere
# Posted by JT · April 22, 2011

Pere, move your assignment of Titl into the code block at top as suggested. This technique does work and it's great. Now I can use proper templates with classic ASP.

Reply

JT
# Posted by Rod · May 3, 2011

Thanks so much John - exactly what I need as well.

Reply

Rod
# Posted by Pico Genkaku · November 29, 2014

Thanks for this post! I've managed to create my draft website using Classic ASP and using master pages =)

Reply

Pico Genkaku
# Posted by Mickey Cutshall · April 4, 2016

Informative suggestions . I Appreciate the analysis - Does anyone know if I might be able to grab a sample IRS W-3 example to use ?

Reply

Mickey Cutshall
# Posted by Stuart · March 15, 2017

forget that .net core. i'm going back to classic asp. this is great stuff. thanks!

Reply

Stuart
Leave a Comment

All comments are moderated and rel="nofollow" is in use. Avatars are sourced from gravatar.com – a globally recognised avatar.

Type the numbers from the picture above

About me
John Avis ...mostly about web development and programming, with a little bit of anything else related to the Internet, computers and technology.

profile for John at Stack Overflow, Q&A for professional and enthusiast programmers
Subscribe

Get the latest posts delivered to your inbox. *