Skip to main content
Participant
June 18, 2020
Question

cfinclude in Application.cfc - variable scope

  • June 18, 2020
  • 3 replies
  • 340 views

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?

 

    This topic has been closed for replies.

    3 replies

    BKBK
    Community Expert
    Community Expert
    June 26, 2020

    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?

    guccio1Author
    Participant
    June 18, 2020

    TY, sir.  I will give it a shot.

     

    BKBK
    Community Expert
    Community Expert
    July 12, 2020

    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>

     

     

     

     

     

    guccio1Author
    Participant
    July 13, 2020

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

    Charlie Arehart
    Community Expert
    Community Expert
    June 18, 2020

    Yes, you can use the "request" scope.

    /Charlie (troubleshooter, carehart. org)