如何从 .cfc 文件调用 ColdFusion 函数?
How to call a ColdFusion function from a .cfc file?
我有一个 .cfc 文件,其中包含我的所有函数(包装在 <cfcomponent>
标记中),包括这个:
<cffunction name="getFooter" output="false" access="public" returnType="string">
<cfargument name="showFooter" type="string" required="false" default="" />
<cfreturn footer />
<cfset application.lib.getFooter(arguments.footer)>
</cffunction>
<cfset footer = getFooter(footer="<footer class="text-center">I am a footer.</footer>") />
在我的 .cfm 文件中,我输入:
<cfoutput>#footer#</cfoutput>
它没有显示页脚。
当我将所有这些都放在一个 .cfm 文件中时,它工作正常。
我需要能够将函数存储在我的 .cfc 文件中并从应用程序的任何位置调用它。
我错过了什么?
我会问和回答我认为你想要的。
问题
我有一个应用程序,它需要有通用的页脚。我的页脚将需要具有某种业务逻辑的能力。现在它只是静态文本。我认为我的页脚需要是一个函数。
我该怎么做?哦,我需要用标签来做这件事,而不是脚本
回答
首先,我们需要一个范围很广的函数。我将把它放在 application.cfc
中。 application.cfc
确实是一个.cfc
,但是非常特别
<!--- application.cfc --->
<cfcomponent>
...
<cffunction name="getFooter">
<cfsavecontent variable="local.result">
</cfsavecontent>
<cfreturn local.result>
</cffunction>
<cfset application.getFooter = getFooter>
...
</cfcomponent>
现在在每个 .cfm
文件中,您可以
<cfoutput>#application.getFooter()#</cfoutput>
你把你的要求说清楚了:
I need to be able to...call it from anywhere in the application.
所以,不要这样做:
"have a .cfc file with all my functions in it (wrapped in a
tag), including this one:"
“从应用程序中的任何位置调用它”的要求仅暗示 ColdFusion 中的一件事:应用程序范围的变量。
因此,改为执行以下操作:将页脚功能从该 CFC 转移到您的 Application.cfc。
我们假设以下是您的 Application.cfc.
的节选
然后,执行如下操作:
<cfcomponent displayname="Application" output="true" hint="Handle the application.">
<!--- Set up the application. --->
<cfset this.Name = "AppCFC" />
<cfset this.ApplicationTimeout = createTimeSpan( 1, 0, 0, 0 ) />
<cfset this.sessionTimeout = createTimeSpan( 0, 0, 30, 0 ) />
<cfset this.sessionManagement = true />
<cffunction
name="OnApplicationStart"
access="public"
returntype="boolean"
output="false"
hint="Fires when the application is first created.">
<!--- This variable is available to every CFM page in the application --->
<cfset application.footer=getFooter()>
<cfreturn true />
</cffunction>
<cffunction
name="getFooter"
output="false"
access="public"
returnType="string">
<cfset var footer = "<footer class=""text-center"">I am a footer.</footer>" />
<cfreturn footer />
</cffunction>
</cfcomponent>
然后,在应用程序的任何 CFM 文件中:
<cfoutput>#application.footer#</cfoutput>
你说过,we store all of our functions in a lib.cfc file.
。因此,在该文件中,编写您的函数。
<cffunction name = "writeAppABCFooter" <!---descriptive name in case there is another footer --->
access = "public"
output = "yes" <!--- this is important --->
returntype = "void">
html code for footer
</cffunction>
要调用您的函数,请先创建一个 lib.cfc 的对象。
<cfobject name = "FooterObject" component = "lib">
然后调用函数。
<cfset FooterObject.writeAppABCFooter()>
这假设 lib.cfc 存在于任何应用程序都可以调用它的位置。
事实证明,这比我们大多数人想象的要简单得多。 <cfsavecontent>
标签不是必需的。不需要将 HTML 存储在变量中。我不知道是不是我的错,但这太复杂了。
最终,这就是我最终要去的地方。
lib.cfc 文件
<cffunction name="getFooter" output="true" access="public" returntype="string">
<footer>I am a footer.</footer>
</cffunction>
index.cfm 文件
<cfoutput>
#application.lib.getFooter()#
</cfoutput>
另一种剪裁方式:
Application.cfc
<cfcomponent displayname="Application" output="false" hint="Handle the application.">
<!--- Set up the application. --->
<cfset this.Name = "AppCFC" />
<cfset this.ApplicationTimeout = createTimeSpan( 1, 0, 0, 0 ) />
<cfset this.sessionTimeout = createTimeSpan( 0, 0, 30, 0 ) />
<cfset this.sessionManagement = true />
<cffunction
name="OnApplicationStart"
access="public"
returntype="boolean"
output="false"
hint="Fires when the application is first created.">
<!--- These variables are available to every CFM page in the application --->
<cfset application.lib = new path_to_Lib_CFC()>
<cfset application.footer=application.lib.getFooter()>
<cfreturn true />
</cffunction>
</cfcomponent>
Lib.cfc
<cfcomponent displayname="Lib" hint="Library of application CFCs">
<cffunction
name="getFooter"
output="false"
access="public"
returnType="string">
<cfset var footer = "<footer class=""text-center"">I am a footer.</footer>" />
<cfreturn footer />
</cffunction>
</cfcomponent>
然后,在应用程序的任何 CFM 文件中:
<cfoutput>#application.footer#</cfoutput>
我发现将简单的可重复使用的东西放在他们自己的 CFM 中并在需要时包含它们很有用。
/app/includes/footer.cfm
<p>I am the footer</p>
index.html
<cfinclude template="app/includes/footer.cfm">
我有一个 .cfc 文件,其中包含我的所有函数(包装在 <cfcomponent>
标记中),包括这个:
<cffunction name="getFooter" output="false" access="public" returnType="string">
<cfargument name="showFooter" type="string" required="false" default="" />
<cfreturn footer />
<cfset application.lib.getFooter(arguments.footer)>
</cffunction>
<cfset footer = getFooter(footer="<footer class="text-center">I am a footer.</footer>") />
在我的 .cfm 文件中,我输入:
<cfoutput>#footer#</cfoutput>
它没有显示页脚。
当我将所有这些都放在一个 .cfm 文件中时,它工作正常。
我需要能够将函数存储在我的 .cfc 文件中并从应用程序的任何位置调用它。
我错过了什么?
我会问和回答我认为你想要的。
问题
我有一个应用程序,它需要有通用的页脚。我的页脚将需要具有某种业务逻辑的能力。现在它只是静态文本。我认为我的页脚需要是一个函数。
我该怎么做?哦,我需要用标签来做这件事,而不是脚本
回答
首先,我们需要一个范围很广的函数。我将把它放在 application.cfc
中。 application.cfc
确实是一个.cfc
,但是非常特别
<!--- application.cfc --->
<cfcomponent>
...
<cffunction name="getFooter">
<cfsavecontent variable="local.result">
</cfsavecontent>
<cfreturn local.result>
</cffunction>
<cfset application.getFooter = getFooter>
...
</cfcomponent>
现在在每个 .cfm
文件中,您可以
<cfoutput>#application.getFooter()#</cfoutput>
你把你的要求说清楚了:
I need to be able to...call it from anywhere in the application.
所以,不要这样做:
"have a .cfc file with all my functions in it (wrapped in a tag), including this one:"
“从应用程序中的任何位置调用它”的要求仅暗示 ColdFusion 中的一件事:应用程序范围的变量。
因此,改为执行以下操作:将页脚功能从该 CFC 转移到您的 Application.cfc。
我们假设以下是您的 Application.cfc.
的节选然后,执行如下操作:
<cfcomponent displayname="Application" output="true" hint="Handle the application.">
<!--- Set up the application. --->
<cfset this.Name = "AppCFC" />
<cfset this.ApplicationTimeout = createTimeSpan( 1, 0, 0, 0 ) />
<cfset this.sessionTimeout = createTimeSpan( 0, 0, 30, 0 ) />
<cfset this.sessionManagement = true />
<cffunction
name="OnApplicationStart"
access="public"
returntype="boolean"
output="false"
hint="Fires when the application is first created.">
<!--- This variable is available to every CFM page in the application --->
<cfset application.footer=getFooter()>
<cfreturn true />
</cffunction>
<cffunction
name="getFooter"
output="false"
access="public"
returnType="string">
<cfset var footer = "<footer class=""text-center"">I am a footer.</footer>" />
<cfreturn footer />
</cffunction>
</cfcomponent>
然后,在应用程序的任何 CFM 文件中:
<cfoutput>#application.footer#</cfoutput>
你说过,we store all of our functions in a lib.cfc file.
。因此,在该文件中,编写您的函数。
<cffunction name = "writeAppABCFooter" <!---descriptive name in case there is another footer --->
access = "public"
output = "yes" <!--- this is important --->
returntype = "void">
html code for footer
</cffunction>
要调用您的函数,请先创建一个 lib.cfc 的对象。
<cfobject name = "FooterObject" component = "lib">
然后调用函数。
<cfset FooterObject.writeAppABCFooter()>
这假设 lib.cfc 存在于任何应用程序都可以调用它的位置。
事实证明,这比我们大多数人想象的要简单得多。 <cfsavecontent>
标签不是必需的。不需要将 HTML 存储在变量中。我不知道是不是我的错,但这太复杂了。
最终,这就是我最终要去的地方。
lib.cfc 文件
<cffunction name="getFooter" output="true" access="public" returntype="string">
<footer>I am a footer.</footer>
</cffunction>
index.cfm 文件
<cfoutput>
#application.lib.getFooter()#
</cfoutput>
另一种剪裁方式:
Application.cfc
<cfcomponent displayname="Application" output="false" hint="Handle the application.">
<!--- Set up the application. --->
<cfset this.Name = "AppCFC" />
<cfset this.ApplicationTimeout = createTimeSpan( 1, 0, 0, 0 ) />
<cfset this.sessionTimeout = createTimeSpan( 0, 0, 30, 0 ) />
<cfset this.sessionManagement = true />
<cffunction
name="OnApplicationStart"
access="public"
returntype="boolean"
output="false"
hint="Fires when the application is first created.">
<!--- These variables are available to every CFM page in the application --->
<cfset application.lib = new path_to_Lib_CFC()>
<cfset application.footer=application.lib.getFooter()>
<cfreturn true />
</cffunction>
</cfcomponent>
Lib.cfc
<cfcomponent displayname="Lib" hint="Library of application CFCs">
<cffunction
name="getFooter"
output="false"
access="public"
returnType="string">
<cfset var footer = "<footer class=""text-center"">I am a footer.</footer>" />
<cfreturn footer />
</cffunction>
</cfcomponent>
然后,在应用程序的任何 CFM 文件中:
<cfoutput>#application.footer#</cfoutput>
我发现将简单的可重复使用的东西放在他们自己的 CFM 中并在需要时包含它们很有用。
/app/includes/footer.cfm
<p>I am the footer</p>
index.html
<cfinclude template="app/includes/footer.cfm">