Search code examples
coldfusionfunction-callcffunction

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


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?


Solution

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