Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variable scope in CF 10 versus CF MX7

I am running into a weird issue with my ColdFusion 10 code. I am new to ColdFusion, so go easy on me. The reason it is weird is because it does not seem to occur in older versions of this platform (i.e. MX 7).

A little info first:

I have two environments. A ColdFusion 10 and a ColdFusion MX 7 (IIS 7 and IIS 5, respectively). In the ColdFusion 10 environment, I have an Application.cfc file with the following statement...

<cfset CompanyLogoText = "Acme Company">

This Application.cfc file is in the web root (mydomain.com). I also have a CFM file in a sub folder of the web root at mydomain.com/pages/default.cfm. It contains the following markup...

<cfoutput><p>#CompanyLogoText#</p></cfoutput>

The issue

When I navigate to mydomain.com/pages/default.cfm, I get an error from coldfusion. The error is "Variable COMPANYLOGOTEXT is undefined."

The weird part

I am not getting this error in the ColdFusion MX 7. The only difference is that the CF MX 7 environment uses a Application.cfm file, but with the same exact line.

Question

How can I get the pages/default.cfm file to see my variable CompanyLogoText in the CF 10 environment?

Here is the full markup

Application.cfc

<cfcomponent>
<cfset This.name = "test_cf">
<cfset This.Sessionmanagement="yes">
<cfset This.Sessiontimeout="#createtimespan(0,0,10,0)#">
<cfset This.applicationtimeout="#createtimespan(5,0,0,0)#">
<cfset This.setclientcookies="no" >
<cfset This.clientmanagement="no">


<cffunction name="onApplicationStart">

    <cfset CompanyLogoText = "Acme Company">    

</cffunction>



<cffunction name="onRequestStart">
    <cfargument name="requestname" required=true />
    <cfset CompanyLogoText = "Acme Company">    


</cffunction>


</cfcomponent>

Pages/Default.cfm

<cftry>

<cfoutput><p>#CompanyLogoText#</p></cfoutput>

<cfcatch>

<p>Could not read CompanyLogoText<br/><br/>

<cfoutput>
<br/>Message:  #cfcatch.message#
<br/>Details:  #cfcatch.detail#.

</cfoutput>

</cfcatch>

</cftry>
like image 211
RyanCJI Avatar asked Mar 14 '14 19:03

RyanCJI


1 Answers

That's the difference between Application.cfm and Application.cfc

Use onRequest(), set the variables, then cfinclude the target file. That's the only way to share the variables scope.

https://wikidocs.adobe.com/wiki/display/coldfusionen/onRequest

e.g.

<cffunction name="onRequest" returnType="void"> 
    <cfargument name="targetPage" type="String" required=true/> 

    <cfinclude template="globalVars.cfm">

    <cfset variables.foo = "bar">

    <cfinclude template="#Arguments.targetPage#">
</cffunction>

QUOTE: CF8: Migrating from Application.cfm to Application.cfc

Put in the onRequest method any code that sets Variables scope variables and add a cfinclude tag that includes the page specified by the method's Arguments.Targetpage variable.

like image 87
Henry Avatar answered Oct 03 '22 03:10

Henry