Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Equivalent to master pages in ASP Classic

Is it possible to build some kind of master page with Classic ASP without frames or iframes?

I’m wondering if there is a way to include content pages in the main page like ASP.NET master pages do. From what I have researched, ASP Classic does support inclusion of other ASP/HTML pages into a page, but the value put into this include means the function cannot be dynamic.

like image 641
Poku Avatar asked May 14 '10 19:05

Poku


People also ask

What is a master page in ASP?

A master page is an ASP.NET file with the extension . master (for example, MySite. master) with a predefined layout that can include static text, HTML elements, and server controls. The master page is identified by a special @ Master directive that replaces the @ Page directive that is used for ordinary .

Is ASP classic still used?

It used scripting on the server to create content that would then be sent to a client's web browser, and it enjoyed a tremendous amount of success in its time as a result. Classic ASP, however, is no longer being developed by Microsoft at all - it has long since been replaced by ASP.NET in 2002, a newer alternative.

How is a master page different from an ASP.NET page?

How is a Master Page different from an ASP.NET page? The basic difference is that master page can't render itself (As its a layout ) but other Asp.net pages can. Master pages are something like abstract class and also it is just a template or layout.

What is difference between master page and content page?

The master page defines placeholders for the content, which can be overridden by content pages. The output result is a combination of the master page and the content page. The content pages contain the content you want to display.


3 Answers

You could just create functions (say, a Header() function and a Footer() function) which do nothing but output some set of markup. Those functions can take parameters too, and be called conditionally. It's not quite the same as a Master page, but it sounds like it accomplishes what you are trying to do. You would have an <!--#include file="headerfooter.asp"--> on each page, and each page would call Header() & Footer().

Or you can just use <!--#include file="header.asp"--> at the top and <!--#include file="footer.asp"--> at the bottom of each page too. I've seen both approaches.

If you are looking for the reverse, that is, a single template page which calls individual pages in it's "middle" section, then that's not really something you can do easily with ASP classic. It's a fundamental difference in approach: ASP.NET has a concept of a control tree, events, etc, while ASP Classic is essentially just a script that runs top to bottom.

like image 137
Matthew Groves Avatar answered Nov 23 '22 17:11

Matthew Groves


This idea is from Classic ASP Master Pages | Godless Code. I’ve transcribed the code in images on that page, extended its example a bit, and also explored the limitations of this technique.

The idea is that each page has only one Server-Side Include (one <!--#include file="" --> call). The single inclusion is a master template file, which you could name master.asp. The master page calls custom subroutines on each page in place of each content area. Each child page defines those subroutines with Sub, with content unique to that child page.

master.asp
<!DOCTYPE html>
<html>
    <head>
        <title><% Title() %></title>
    </head>
    <body>
        <% BodyContent() %>
    </body>
</html>
aboutUs.asp
<!--#include file="master.asp" -->

<% Sub Title %> About Us <% End Sub %>

<% Sub BodyContent %>
    <h1>About Us</h1>
    <p>
        We do things!
    </p>
<% End Sub %>

That turns into this HTML when you visit aboutUs.asp on an IIS server:

<!DOCTYPE html>
<html>
    <head>
        <title> About Us </title>
    </head>
    <body>

    <h1>About Us</h1>
    <p>
        We do things!
    </p>

    </body>
</html>

However, this approach does not allow nesting:

subtemplate.asp
<div class="innerLogo <% LogoSide() %>">
    <% LogoImg() %>
</div>
template_user.asp
<!--#include file="master.asp" -->

<% Sub Title %> Our Logo <% End Sub %>

<% Sub BodyContent %>

    <!--#include file="subtemplate.asp" -->

    <% Sub LogoSide %> leftside <% End Sub %>

    <% Sub LogoImg %>
        <img src="img/about.png" alt="About" />
    <% End Sub %>

<% End Sub %>

This will not work, because nested Subs are a syntax error:

Microsoft VBScript compilation error '800a03ea'

Syntax error

/template_user.asp, line 9

Sub LogoSide
^

Since nesting is not allowed, this templating system is, in effect, a one-time solution. If your individual pages’ subroutines become too unwieldy, you can’t use this technique again. So when using this technique, you should carefully choose where to carve out your set of templates in order to provide the best balance between flexibility and DRYness.

like image 23
Rory O'Kane Avatar answered Nov 23 '22 18:11

Rory O'Kane


Rory wrote a great example for master pages in Classic ASP, but demonstrated that the "master page" approach had its limitations because Subs cannot be nested.

However, for the sake of demonstration, and because JavaScript in Classic ASP has hardly any documentation anywhere on the Internet, here's the same example that fails in ASP VBScript but won't fail in ASP JavaScript.

master.asp
<!DOCTYPE html>
<html>
    <head>
        <title><% Title() %></title>
    </head>
    <body>
        <% BodyContent() %>
    </body>
</html>
subtemplate.asp
<div class="innerLogo <% LogoSide() %>">
    <% LogoImg() %>
</div>
template_user.asp
<%@ Language= "Javascript" %> 
<!--#include file="master.asp" -->

<% function Title() { %> About Us <% } %>

<% function BodyContent() { %>

    <!--#include file="subtemplate.asp" -->

    <% function LogoSide() { %> leftside <% } %>

    <% function LogoImg() { %>
        <img src="img/about.png" alt="About" />
    <% } %>
<% } %>

It works! here are the juicy results:

<!DOCTYPE html>
<html>
    <head>
        <title> About Us </title>
    </head>
    <body>

    <div class="innerLogo  leftside ">
      <img src="img/about.png" alt="About" />
    </div>

    </body>
</html>

Remember, JavaScript, even the ECMAScript 3 version in Classic ASP, is often way more powerful and expressive than the VBScript engine that was favoured and heavily promoted by Microsoft. If you ever have to use Classic ASP, use JavaScript!

like image 39
Matthew Dean Avatar answered Nov 23 '22 19:11

Matthew Dean