相当于 ASP Classic 中的母版页

发布于 2024-09-02 06:17:15 字数 307 浏览 8 评论 0原文

是否可以使用经典 ASP 不使用框架或 iframe 构建某种母版页?

我想知道是否有办法在主页中包含内容页面,例如 ASP.NET 母版页 可以。根据我的研究,ASP Classic 确实支持将其他 ASP/HTML 页面包含到页面中,但是放入此 include 中的值意味着该函数不能是动态的。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

野稚 2024-09-09 06:17:15

您可以只创建函数(例如,一个 Header() 函数和一个 Footer() 函数),它们除了输出一些标记之外什么也不做。这些函数也可以带参数,并有条件地调用。它与母版页不太一样,但听起来它可以完成您想要做的事情。每个页面上都会有一个 ,并且每个页面都会调用 Header() & 页脚()

或者您可以在顶部使用 也位于每个页面的底部。我见过这两种方法。

如果您正在寻找相反的情况,即单个模板页面调用其“中间”部分中的各个页面,那么这并不是您可以使用 ASP classic 轻松完成的事情。这是方法上的根本区别:ASP.NET 有控制树、事件等概念,而 ASP Classic 本质上只是一个从上到下运行的脚本。

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.

风情万种。 2024-09-09 06:17:15

这个想法来自经典ASP母版页|无神代码。我已经在该页面上的图像中转录了代码,稍微扩展了它的示例,并且还探讨了该技术的局限性。

这个想法是每个页面只有一个服务器端包含(一个 调用)。单个包含内容是一个主模板文件,您可以将其命名为 master.asp。母版页调用每个页面上的自定义子例程来代替每个内容区域。每个子页面都使用 Sub 定义这些子例程,并包含该子页面特有的内容。

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 %>

当您在 IIS 服务器上访问 aboutUs.asp 时,它会变成以下 HTML:

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

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

    </body>
</html>

但是,这种方法不允许嵌套:

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 %>

这不会起作用,因为嵌套的 Sub 是语法错误:

Microsoft VBScript 编译错误“800a03ea”

语法错误

/template_user.asp,第 9 行

子 LogoSide
^

由于不允许嵌套,因此该模板系统实际上是一种一次性解决方案。如果您的各个页面的子例程变得过于笨拙,您就无法再次使用此技术。因此,在使用此技术时,您应该仔细选择在何处创建模板集,以便在灵活性和 之间提供最佳平衡干燥。

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.

网名女生简单气质 2024-09-09 06:17:15

Rory 为经典 ASP 中的母版页编写了一个很好的示例,但证明“母版页”方法有其局限性,因为子对象不能嵌套。

然而,为了演示,并且由于经典 ASP 中的 JavaScript 在 Internet 上几乎没有任何文档,因此这里有一个在 ASP VBScript 中失败但在 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" />
    <% } %>
<% } %>

有用!以下是有趣的结果:

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

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

    </body>
</html>

请记住,JavaScript,甚至经典 ASP 中的 ECMAScript 3 版本,通常比 Microsoft 青睐和大力推广的 VBScript 引擎更强大、更具表现力。如果您必须使用经典 ASP,请使用 JavaScript!

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!

恬淡成诗 2024-09-09 06:17:15

经典 ASP 中最丑陋的问题之一是 #includes 总是发生,因此将两个包含放在 if-then-else 中 构造始终包含两者 - 即使您只看到适用于条件值的输出。

即使包含工作,它们也不会为您提供真正想要的结果,即“即时”选择模板或皮肤。

处理这种情况的一种方法是使用超越传统 #include 方法的模板引擎,例如 KudzuASP。下面是一个非常简单的示例:

<!-- An HTML Template -->
<html>
<head><title><!--[Replace|PageTitle]-->PageTitle<!--[/Replace]--></title></head>
<body>
<table border="1" cellpadding="4" callspacing="2" width="640">
<tr>
    <td colspan="2"><!--[HeaderContent/]--></td>
</tr>
<tr>
    <td width="160"><!--[LeftColumnContent/]--></td>
    <td><!--[MainContent/]--></td>
</tr>
<tr>
    <td colspan="2"><!--[FooterContent/]--></td>
</tr>
</table>
</body>
</html>

ASP 代码如下所示:

<%@ Language=VBScript %>
<!-- #include file="./KudzuASP/_kudzu.asp" -->
<%
Dim PageTitle : PageTitle = "This is a Master Page"

'
' Create the template engine
'
Dim T_ENGINE
Set T_ENGINE = New CTemplateEngine

T_ENGINE.PutValue "PageTemplate", PageTemplate
T_ENGINE.SetHandler "HeaderContent", New CTXHeaderContent
T_ENGINE.SetHandler "LeftColumnContent", New CTXLeftColumnContent
T_ENGINE.SetHandler "MainContent", New CTXMainContent
T_ENGINE.SetHandler "FooterContent", New CTXFooterContent

'
' Custom Tage Handlers
'
Class CTXHeaderContent
    Public Sub HandleTag(vNode)
        vNode.Engine.ContentAppend "Header"
    End Sub
End Class

Class CTXLeftColumnContent
    Public Sub HandleTag(vNode)
        vNode.Engine.ContentAppend "Left<br/>Content"
    End Sub
End Class

Class CTXMainContent
    Public Sub HandleTag(vNode)
        vNode.Engine.ContentAppend "Main<br/>Content"
    End Sub
End Class

Class CTXFooterContent
    Public Sub HandleTag(vNode)
        vNode.Engine.ContentAppend "Footer"
    End Sub
End Class

'
' Evaluate the template
'
T_ENGINE.ParseFile Server.MapPath("./MasterPage.html")
T_ENGINE.EvalTemplate
%>

当处理适当的标记时,模板引擎将调用托管 ASP 代码页中定义的自定义对象。自定义类的函数成员可以直接访问托管页面及其变量和方法,以及模板引擎的对象层次结构。换句话说,模板在输出期间驱动输出和托管 ASP 页。

这轻而易举地击败了包含机制,因为模板引擎可以在运行时动态选择要处理的 HTML 模板,并且它可以使用内置的

更新2016.01.13:我已经开源了这个项目,你可以在这个地址找到维护的最新代码:https:// github.com/Mumpitz/KudzuASP

One of the ugliest problems in classic ASP is that #includes always happen, so putting two includes in an if-then-else construct always includes both – even though you only see the output that applies to your conditional value.

Even when includes work, they don't give you the result you're really looking for, which is to select a template or skin “on the fly”.

One way to handle this situation is to use a template engine such as KudzuASP that surpasses the traditional #include methodology. Here is a very simple example:

<!-- An HTML Template -->
<html>
<head><title><!--[Replace|PageTitle]-->PageTitle<!--[/Replace]--></title></head>
<body>
<table border="1" cellpadding="4" callspacing="2" width="640">
<tr>
    <td colspan="2"><!--[HeaderContent/]--></td>
</tr>
<tr>
    <td width="160"><!--[LeftColumnContent/]--></td>
    <td><!--[MainContent/]--></td>
</tr>
<tr>
    <td colspan="2"><!--[FooterContent/]--></td>
</tr>
</table>
</body>
</html>

And the ASP code looks like this:

<%@ Language=VBScript %>
<!-- #include file="./KudzuASP/_kudzu.asp" -->
<%
Dim PageTitle : PageTitle = "This is a Master Page"

'
' Create the template engine
'
Dim T_ENGINE
Set T_ENGINE = New CTemplateEngine

T_ENGINE.PutValue "PageTemplate", PageTemplate
T_ENGINE.SetHandler "HeaderContent", New CTXHeaderContent
T_ENGINE.SetHandler "LeftColumnContent", New CTXLeftColumnContent
T_ENGINE.SetHandler "MainContent", New CTXMainContent
T_ENGINE.SetHandler "FooterContent", New CTXFooterContent

'
' Custom Tage Handlers
'
Class CTXHeaderContent
    Public Sub HandleTag(vNode)
        vNode.Engine.ContentAppend "Header"
    End Sub
End Class

Class CTXLeftColumnContent
    Public Sub HandleTag(vNode)
        vNode.Engine.ContentAppend "Left<br/>Content"
    End Sub
End Class

Class CTXMainContent
    Public Sub HandleTag(vNode)
        vNode.Engine.ContentAppend "Main<br/>Content"
    End Sub
End Class

Class CTXFooterContent
    Public Sub HandleTag(vNode)
        vNode.Engine.ContentAppend "Footer"
    End Sub
End Class

'
' Evaluate the template
'
T_ENGINE.ParseFile Server.MapPath("./MasterPage.html")
T_ENGINE.EvalTemplate
%>

The template engine makes calls to your custom objects defined in the hosting ASP code page when the appropriate tags are processed. The function members of your custom classes have direct access to the hosting page and its variables and methods, as well as the template engine’s object hierarchy. In other words, the template is driving the output and the hosting ASP page during output.

This beats the include mechanism hands down, because the template engine can dynamically select which HTML template to process at runtime, and it can dynamically include libraries of custom tag handlers using the built in <!--[import/]--> tag.

UPDATE 2016.01.13: I have open sourced this project and you can find the latest code maintained at this address: https://github.com/Mumpitz/KudzuASP

隱形的亼 2024-09-09 06:17:15

我只是使用带有 html 的 Default.asp 页面,然后将我的代码放在内容区域中。

<%@ Language="VBScript" %>
    <!DOCTYPE html>
    <html lang="en">
        <head>
             <meta charset="utf-8" />
        </head>
        <body>
            <div id="topNav"> <!--begin top Nav-->
                <ul> 
                    <!--Be sure that all links are like this href="?page=contentPageEx"-->
                    <li><a href="?page=home">Home</a></li> 
                </ul>   
            </div> <!--end top Nav-->

            <div id="content">
                   <%
                        Dim default
                        default= Request.QueryString 
                        If default= "" Then 
                        Server.execute "includes/home.html"
                        Else 
                        Server.execute "includes/" & request("page")  & ".html"
                        end if 
                    %>   
            </div>  


            <div id="botNav"> <!--begin bot Nav-->
                <ul> 
                  <li><a href="?page=home">Home</a></li>   
                </ul>
            </div> <!--end Bot Nav-->

         </body>
</html> 

然后我将所有内容放入包含 html 页面的包含文件中。

  <!DOCTYPE html>

    <html lang="en">
        <head>
            <meta charset="utf-8" />

            <!--Search engines use this title ect...-->
            <title>Hello SEO! This is a content page!</title>
            <!--Can be styled independently-->
            <style>
              p {
                    color: #0094ff;   
                }  
            </style>
        </head>
        <body>
         <p>Hello World!</p>   
        </body>
    </html> 

I just use a Default.asp page with html, then put my code in the content area.

<%@ Language="VBScript" %>
    <!DOCTYPE html>
    <html lang="en">
        <head>
             <meta charset="utf-8" />
        </head>
        <body>
            <div id="topNav"> <!--begin top Nav-->
                <ul> 
                    <!--Be sure that all links are like this href="?page=contentPageEx"-->
                    <li><a href="?page=home">Home</a></li> 
                </ul>   
            </div> <!--end top Nav-->

            <div id="content">
                   <%
                        Dim default
                        default= Request.QueryString 
                        If default= "" Then 
                        Server.execute "includes/home.html"
                        Else 
                        Server.execute "includes/" & request("page")  & ".html"
                        end if 
                    %>   
            </div>  


            <div id="botNav"> <!--begin bot Nav-->
                <ul> 
                  <li><a href="?page=home">Home</a></li>   
                </ul>
            </div> <!--end Bot Nav-->

         </body>
</html> 

Then I put all my content into an includes file with html pages.

  <!DOCTYPE html>

    <html lang="en">
        <head>
            <meta charset="utf-8" />

            <!--Search engines use this title ect...-->
            <title>Hello SEO! This is a content page!</title>
            <!--Can be styled independently-->
            <style>
              p {
                    color: #0094ff;   
                }  
            </style>
        </head>
        <body>
         <p>Hello World!</p>   
        </body>
    </html> 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文