Highlighted

cfinclude in Application.cfc - variable scope

New Here ,
Jun 18, 2020

Copy link to clipboard

Copied

I am doing a cfinclude in my Application.cfc and the include sets variables in a cfscript.

 

<cffunction name="OnRequestStart">
        <cfinclude template="encryption.cfm">

</cffunction>

 

The varialbes that are set are known within the Application.cfc but are not available to the .cfm that made the call to the Application.cfc

 

Is there a workaround?

 

Views

74

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

cfinclude in Application.cfc - variable scope

New Here ,
Jun 18, 2020

Copy link to clipboard

Copied

I am doing a cfinclude in my Application.cfc and the include sets variables in a cfscript.

 

<cffunction name="OnRequestStart">
        <cfinclude template="encryption.cfm">

</cffunction>

 

The varialbes that are set are known within the Application.cfc but are not available to the .cfm that made the call to the Application.cfc

 

Is there a workaround?

 

Views

75

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Jun 18, 2020 0
Adobe Community Professional ,
Jun 18, 2020

Copy link to clipboard

Copied

Yes, you can use the "request" scope.

/Charlie (server troubleshooter, carehart.org)

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 18, 2020 1
New Here ,
Jun 18, 2020

Copy link to clipboard

Copied

TY, sir.  I will give it a shot.

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 18, 2020 0
Adobe Community Professional ,
Jul 12, 2020

Copy link to clipboard

Copied

Hi guccio1,

 

How did it go?

 

Looking again at your statement,

"The varialbes that are set are known within the Application.cfc but are not available to the .cfm that made the call to the Application.cfc"

 

here are some more remarks:

 

1) if you place the line

 

<cfset testVar=1><!--- Equivalent to: <cfset variables.testVar=1>--->

 

in onRequestStart, then testVar will be defined in Application.cfc, but it will not be defined on somePage.cfm.

 

2) the behaviour you observed therefore had nothing to do with your use of cfinclude or cfscript. 

 

3) There are many workarounds (to make a variable that is set in onRequestStart to be available to any arbitrary CFM page). Some workarounds involve the use of scope, each scope depending on the use-case you want to implement. Charlie has given you the usual one:

 

<cfset request.testVar=1>

 

Others involve the following scopes:

 

Scope Use case
client (assuming client management has been enabled)
session (assuming session management has been enabled)

To initialise a variable that applies to a specific user

cookie To initialise a cookie variable; available to every user and every page of the application
form To initialise a form variable; available to every user and every page of the application
url To initialise a variable passed as query-string in the URL; available to every user and every page of the application
application To initialise an application variable, available to every user and every page of the application
server To initialise a server variable, available to every application; rarely if ever user

 

There is yet another workaround. In this one, you keep your code as it currently is, with the cfinclude in onRequestStart:

 

<cffunction name="onRequestStart" returntype="boolean">
 <cfargument name = "targetPage" type="String" required="true"> 
 <cfinclude template="encryption.cfm">
<cfreturn true>
</cffunction>

<cffunction name="onRequest">
<cfargument type="String" name = "targetPage" required=true/>
<cfinclude template="#Arguments.targetPage#">
</cffunction>

 

 

 

 

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jul 12, 2020 0
New Here ,
Jul 13, 2020

Copy link to clipboard

Copied

Many thanks to all.  I was able to resolve this using an onRequest function in addition to onRequestStart.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jul 13, 2020 0
BKBK LATEST
Adobe Community Professional ,
Jul 13, 2020

Copy link to clipboard

Copied

Hi guccio1 ,

I am glad to hear that. Please kindly mark the correct answer. You may of course mark your own. 🙂

The main thing is that a post marked as answered will help someone else in future.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jul 13, 2020 0
Adobe Community Professional ,
Jun 26, 2020

Copy link to clipboard

Copied

Hi guccio1,

You say that the variables "are known within the Application.cfc but are not available to the .cfm that made the call to the Application.cfc.

Is there a workaround?"

 

It's unclear to me what you want. Do you want the variables to be:

1) known within the Application.cfc and available to the .cfm that made the call to the Application.cfc, or

2) known within the Application.cfc but not available to the .cfm that made the call to the Application.cfc?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 26, 2020 0