templates - 如何使用coldfusion构建母版页?

标签 templates coldfusion logic master-pages

我在我们的网站上有一个小的 Coldfusion 部分,它们都使用类似的 js 和 css 文件和页面结构。该代码目前对每个文件重复,我想将其分解并使用母版页和模板进行设置。

Master.cfm 页面:

<!--- Master template, includes all necessary js and css files. 
    Expects following variables to be defined:
    - pageName - name of the file to be loaded as the body of the page 
    - title - text to be used as the title of the page and as the header text in the header bar --->
<cfinclude template="_lockedPage.cfm" />

<!DOCTYPE HTML>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>#title#</title>
        ... all script and css links here ...
        <script type="text/javascript" src="js/jquery-1.9.1.min.js"></script>
        <script type="text/javascript" src="js/jquery.mobile-1.3.2.js"></script>
        ... etc ...
    </head>
    <body>
        <div data-role="page">
            <div class="headerDiv" data-role="header" data-theme="b" data-position="fixed">
                <a id="backButton" data-role="button" data-direction="reverse" data-rel="back" data-icon="arrow-l" data-iconpos="left" data-theme="a">Back</a>
                <h1><cfoutput>#title#</cfoutput></h1>
                <a href="index.cfm" data-role="button" data-icon="home" data-iconpos="left" data-theme="a">Home</a>
            </div>
            <div data-role="content" class="container">
                <cfinclude template="#pageName#.cfm" />
            </div>
        </div>
    </body>
</html>

那么页面示例将是这样的。客户搜索.cfm:
<cfscript>
    title = "Customer Search";
    pageName = "_customer-search";
    include "Master.cfm";
</cfscript>

然后我需要一个 _customer-search.cfm 页面,其中包含页面的所有正文内容。

这意味着我们当前拥有的每个页面都需要 2 个文件 - 定义变量并包含母版页的外部页面,以及具有单个页面内容的模板页面。

这是一个好的逻辑结构吗?有没有办法改进它?

最佳答案

您的想法是正确的,但我认为您最终会得到很多不必要的文件。您可以改为创建 header.cfm和一个 footer.cfm包含您的全局 HTML。每个页面都将包含这些文件,并且内容将写入它们之间。

<cfset title = "Customer Search">
<cfinclude template="global_header.cfm">

<!--- This will be the content of your page. --->

<cfinclude template="global_footer.cfm">

该文件将被命名为 customer_search.cfm .无论何时更新页眉或页脚,都是全局更改。

如果您有很多业务逻辑和查询代码需要存在于多个页面上,您可能会考虑使用 MVC 框架来帮助您组织和重用代码。我更喜欢 ColdBox (试试ColdBox Lite),但是很多人用Framework/1 .

关于templates - 如何使用coldfusion构建母版页?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22537428/

相关文章:

C++ 对象池 : program crashes when using malloc

c++ - while (数组末尾) - 如何识别

C 将 int 转换为位移运算符

Java:构造函数中抛出异常,我的对象仍然可以创建吗?

c++ - 类模板需要模板参数列表

c++ - 为什么类外成员模板定义需要重复其声明 'requires-clause'

jquery - 如何使用 ColdFusion/jQuery 在同一页面上发布表单、更新查询并显示结果?

coldfusion - 在 CF2016 中,变量和 This 是否被视为 CFC 内的隐式范围?

java - 需要从 jvm 中删除 fusionreactor 条目

javascript - 未知的函数行为