代码之家  ›  专栏  ›  技术社区  ›  Joshua Carmody

ColdFusion对ASP.NET的母版页有答案吗?

  •  7
  • Joshua Carmody  · 技术社区  · 17 年前

    我正在开发一个用ColdFusion编码的网站。我有一个CSS/HTML模板,我想应用到每个页面的内容,而不需要复制任何多余的代码。我已经被ASP.NET的母版页宠坏了,这是我实现这个站点的首选方法。不幸的是,我没有这个选择。这个网站必须在ColdFusion MX 7上运行。另外,领导这个项目的开发人员不喜欢Fusebox,所以这个选项不可用。

    主导航、图形页眉和页脚在每一页上都是相同的。标题标签、元标签和2级导航可能因页面而异。除此之外,只有页面的“主要内容区域”会有所不同。

    考虑到这些参数,如何对站点进行编码以获得最大的可维护性?

    4 回复  |  直到 9 年前
        1
  •  12
  •   Peter Boughton    17 年前

    用ColdFusion有很多方法可以做到这一点。


    Application.cfc 对每个请求执行,有两种方法( onRequestStart onRequestEnd )可用于在页面中为主脚本添加内容。

    同样值得注意的是,可以扩展/继承application.cfc,从而允许更复杂的requeststart/end事件集。 More details here here .


    Custom Tags 允许您创建一个标记,可以环绕每个模板应用布局/etc。它还允许属性/etc定义通用但可更改的文本。

    例如:

    <cf_page PageTitle="My Page">
        [main page content]
    </cf_page>
    

    在自定义标记(page.cfm)中,您有:

    <cfif ThisTag.ExecutionMode EQ 'start'>
        <cfparam name="Attributes.PageTitle" default=""/>
        <cfcontent reset/><cfoutput><!DOCTYPE html>
        <html>
        <head>
            <title>My Website - #Attributes.PageTitle</title>
            [styles and scripts and stuff]
        </head>
        <body>
            <div id="heading">
                <img src="my_website_logo.png" alt="My Website"/>
            </div>
            <ul id="mainmenu" class="nav">
                [menu]
            </ul>
            <h1>#Attribute.PageTitle#</h1>
        </cfoutput>
    <cfelse>
        <cfoutput>
            <div id="footer">
                [footer]
            </div>
        </body></html></cfoutput>
    </cfif>
    

    当然,您可以创建多个自定义标记,也可以创建一个根据指定属性以多种方式工作的标记。


    亨利已经提到过MVC 框架 ,但您不需要执行MVC来使用模板/布局功能。

    Fusebox 可以做MVC,但不行 要求 您可以这样做,而且另外,FB的ContentVariables是实现模块化内容的一个很好的工具,除非您的主要开发人员能够 证明正当 他不喜欢Fusebox(并建议一个更适合您项目的替代方案!)那么,完全没有理由不去追求它——它是一个成熟而知名的框架,易于使用,开发人员众多,等等。

    但是,如果Fusebox确实不是一个选项,请看一下 Charlie Arehart's list of frameworks -通常,该页面是一个值得查看的工具列表。


    不管怎样,这应该给你足够的东西来考虑现在…

        2
  •  3
  •   Patrick McElhaney    9 年前

    ColdFusion开发人员在90年代后期开始使用一个名为cf_BodyContent的自定义标记,以避免包含单独的页眉和页脚文件。这比ASP.NET的母版页早了六七年。;-)

    现在有一个本地标签可以做同样的事情: CFaveCeCon . 下面是人们如何在模板中使用cfsavecontent的本质。

       <!--- index.cfm --->
       <cfsavecontent variable="content">
          <cfinclude template="#url.action#.cfm">
       </cfsavecontent> 
    
       <cfinclude template="template.cfm">
    
       <!--- template.cfm --->
       <cfparam name="title" default="Welcome">
       <html>
          <head><cfoutput>#title#</cfoutput></head>
          <body>
             ... header, menu, sidebar, whatever ...
             <cfoutput>#content#</cfoutput>
             ... right column, footer ...
          </body>
       </html>
    
       <!--- foo.cfm --->
       <cfset title="Welcome to Foo">
       Hello World! I'm the page at index.cfm?action=foo
    
       <!--- bar.cfm --->
       <cfset title="Welcome to Bar">
       Hello World! I'm the page at index.cfm?action=bar
    

    如果要在模板中放置模板,只需添加另一个cfsavecontent。

       <!--- index.cfm --->
       <cfsavecontent variable="content">
          <cfinclude template="#url.action#.cfm">
       </cfsavecontent> 
    
       <cfsavecontent variable="content">
          <cfinclude template="internal_template.cfm">
       </cfsavecontent>
    
       <cfsavecontent variable="content">
          <cfinclude template="master_template.cfm">
       </cfsavecontent>         
    
       <cfoutput>#content#</cfoutput>         
    

    您可以重构以消除冗余。

       <!--- index.cfm --->
       <cfsavecontent variable="content">
           <cfinclude template="#url.action#.cfm">
       </cfsavecontent> 
    
       <cfparam name="templates" default="internal,master">
    
       <cfloop list="#templates#" index="t">
           <cfsavecontent variable="content">
               <cfinclude template="#t#_template.cfm">
           </cfsavecontent>
       </cfloop> 
    
       <cfoutput>#content#</cfoutput>  
    

    如果您想让一个模板“扩展”另一个模板,您可以通过将列表转换为堆栈,并让每个模板将其父模板推送到堆栈上来实现。

      <!--- internal_template.cfm --->
      <cfset templates = listAppend("master", templates)>  
    
      ...
      <cfoutput>#content#</cfoutput>
      ...
    
    
      <!--- index.cfm --->
      <cfsavecontent variable="content">
          <cfinclude template="#url.action#.cfm">
      </cfsavecontent> 
    
      <cfparam name="templates" default="internal">
    
      <cfloop condition="listlen(templates) gt 0">
          <cfset t = listFirst(templates)>
          <cfset templates = listRest(templates)>
          <cfsavecontent variable="content">
              <cfinclude template="#t#_template.cfm">
          </cfsavecontent>
      </cfloop> 
    
      <cfoutput>#content#</cfoutput> 
    

    这样就有了StackBox,一个在StackOverflow上发明的ColdFusion框架。-)

        3
  •  1
  •   Henry    17 年前

    您可以尝试使用支持模板的MVC框架(几乎每个人都有)。

    ColdBox , Model-Glue , Mach-II , Fusebox

    这个 Galleon Forum Ports Comparisons 页面突出显示了每个框架如何处理模板…

        4
  •  0
  •   Sam Axe    17 年前

    查看cfinclude

    推荐文章