How to call a ColdFusion function from a .cfc file?

1.6k views Asked by At

I have a .cfc file with all my functions in it (wrapped in a <cfcomponent> tag), including this one:

<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>") />

And in my .cfm file, I put:

<cfoutput>#footer#</cfoutput>

It is not displaying the footer.

When I have all of this in a single .cfm file, it works fine.

I need to be able to store the function in my .cfc file and call it from anywhere in the application.

What am I missing?

6

There are 6 answers

0
BKBK On BEST ANSWER

Yet another way to cut it:

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>

Then, in any CFM file in the application:

<cfoutput>#application.footer#</cfoutput>
2
Millhorn On

Turns out this was a lot simpler than most us made it out to be. The <cfsavecontent> tag wasn't necessary. Storing the HTML in a variable wasn't required. I don't know if it was me or not, but this got way over complicated.

Ultimately, this is where I ended up going.

lib.cfc file

<cffunction name="getFooter" output="true" access="public" returntype="string">
    <footer>I am a footer.</footer>
</cffunction>

index.cfm file

<cfoutput>
    #application.lib.getFooter()#
</cfoutput>
0
Dan Bracuk On

You stated, we store all of our functions in a lib.cfc file.. So, in that file, write your function.

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

To call your function, first create an object of lib.cfc.

<cfobject name = "FooterObject" component = "lib">

Then call the function.

<cfset FooterObject.writeAppABCFooter()>

This assumes that lib.cfc exists in a location that enables it to be called by any application.

2
James A Mohler On

I am going to ask and answer what I think you want.

Question

I have an application and it needs to have common footers. My footers are going to need the ability to have some sort of business logic. Right now it is Just static text. I think my footers need to be a function.

How can I do that? Oh, and I need to do this with tags, not script

Answer

First, we need a function with a very wide scope. I am going to put it in application.cfc. It is true that application.cfc is a .cfc, but it is highly special.

<!--- application.cfc --->
<cfcomponent>
...
<cffunction name="getFooter">
    <cfsavecontent variable="local.result">

    </cfsavecontent>

    <cfreturn local.result>
</cffunction>

<cfset application.getFooter = getFooter>
...
</cfcomponent>

Now inside of each of the .cfm files, you can

<cfoutput>#application.getFooter()#</cfoutput>
2
Eric Searing On

I find it useful to just put simple reuseable things in their own CFM and just include them when needed.

/app/includes/footer.cfm

<p>I am the footer</p>

index.html

<cfinclude template="app/includes/footer.cfm">
2
BKBK On

You explain your requirement clearly:

I need to be able to...call it from anywhere in the application.

So, don't do this:

"have a .cfc file with all my functions in it (wrapped in a tag), including this one:"

The requirement, "call it from anywhere in the application", implies just one thing in ColdFusion: an application-scoped variable.

So, do the following instead: transfer the footer functionality from that CFC to your Application.cfc.

Let's assume the following is an excerpt of your Application.cfc.

Then, do something like:

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

Then, in any CFM file in the application:

<cfoutput>#application.footer#</cfoutput>