• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

cfinclude in Application.cfc - variable scope

New Here ,
Jun 18, 2020 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

193

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
community guidelines
Community Expert ,
Jun 18, 2020 Jun 18, 2020

Copy link to clipboard

Copied

Yes, you can use the "request" scope.


/Charlie (troubleshooter, carehart.org)

Votes

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
community guidelines
New Here ,
Jun 18, 2020 Jun 18, 2020

Copy link to clipboard

Copied

TY, sir.  I will give it a shot.

 

Votes

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
community guidelines
Community Expert ,
Jul 12, 2020 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>

 

 

 

 

 

Votes

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
community guidelines
New Here ,
Jul 13, 2020 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.

Votes

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
community guidelines
Community Expert ,
Jul 13, 2020 Jul 13, 2020

Copy link to clipboard

Copied

LATEST

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.

Votes

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
community guidelines
Community Expert ,
Jun 26, 2020 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?

Votes

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
community guidelines
Resources
Documentation