Classic ASP Master Pages
by johna | March 27, 2010 | Classic ASP Web Development
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
content-page.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 %>
Related Posts
Converting dBase IV programs to run in the browser
by johna | September 13, 2024
Some pointless entertainment trying to get some old dBase programs running in the browser.
How to set up a debugging using the Turnkey Linux LAMP stack and VS Code
by johna | December 19, 2023
The second part in my guide to setting up a website and database using the Turnkey Linux LAMP stack.
How to set up a website and database using the Turnkey Linux LAMP stack
by johna | November 18, 2023
If you need to host your own website for the purposes of web development, Turnkey Linux LAMP Stack is an easy to install all-in-one solution that you can set up on a spare computer or a VM (Virtual Machine).
Comments
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
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
by Rod | May 3, 2011
Thanks so much John - exactly what I need as well.
Reply
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
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
by Stuart | March 15, 2017
forget that .net core. i'm going back to classic asp. this is great stuff. thanks!
Reply