Search code examples
coldfusioncoldfusion-8application.cfm

How to include UDF_library in application.cfm?


I am using ColdFusion 8.0.1

I am working on an existing application that has thousands of pages. I am trying to include a library of new UDFs in my application.cfm file.

I addedd this line to application.cfm:

<!--- UDF library include --->
<cfinclude template="UDF/udf_library.cfm">

The UDF library includes other files that contain UDFs, like this:

<cfinclude template="udf_powerreview.cfm">

I have functions in the udf_powerreview.cfm file, such as:

// CREATE POWER REVIEWS SNIPPET
function createPRSnippet(Page_ID) {
    LOCAL.Page_ID =  ARGUMENTS.Page_ID;
    if (isNumeric(LOCAL.Page_ID) && LOCAL.Page_ID > 0) {
        LOCAL.Snippet = "<div class='pr_snippet_product'><script type='text/javascript'>var pr_snippet_min_reviews = 0; POWERREVIEWS.display.snippet(document, { pr_page_id : '#LOCAL.Page_ID#' });</script></div>";
    } else {
        LOCAL.Snippet = "";
    }
    return LOCAL.Snippet;
}

The debugging tool says that UDF/udf_library.cfm and udf_powerreview.cfm are being successfully included.

The problem is when I call the function in another page, I get an error that says that function doesn't exist. When I can copy the function and put it directly into the page that it is used in and it works just fine. And, I do not get the error "routines can not be declared twice".

In every site that I build, I create a udf_library.cfm or udf_library.cfc in the exact same manner. They always work fine.

What might prevent the functions from being available and accessed? Is there an application setting that needs to be set?


Solution

  • It's a page scope issue. Don't think of the Application.cfm as an include on all your pages, just know that it runs first. Somethings it initializes will carry over to your existing page scope and some things won't. Using an Application.cfc instead of an application.cfm takes care of much of the ambiguity.

    To make your UDF's available to your whole application, I would suggest using a "Singleton" Design pattern. First take your UDF's and put them in a CFC format. This will make them more portable.

    in your application.cfm you could put the following lines:

    <cfif NOT isdefined('session.udf_powerreview') or isdefined('url.resetudf')>
      <cfset  session.udf_powerreview = createobject('Component','udf.udf_powerreview')/>
      <!--- this 'udf.udf_powerreview' represents the physical path udf/udf_powerreview.cfc --->
    </cfif>
    

    I'm stuffing it in the session scope instead of the application scope, becuase you won't have an good way of resetting the application scope if you modify your UDF's.

    Either way, once this is in your application.cfm you should be able to see your functions on any page.

    <cfdump var="#session.udf_powerreview#">