Skip to main content
Participant
Jun 18, 2020 4:25 PM UTC
Question

cfinclude in Application.cfc - variable scope

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 is closed to new replies. Start a new post to keep the conversation going.

    6 replies

    BKBK
    Community Expert
    Community Expert
    Jun 26, 2020 1:57 PM UTC

    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?

    Translate
    guccio1Author
    Participant
    Jun 18, 2020 6:07 PM UTC

    TY, sir.  I will give it a shot.

     

    Translate
    BKBK
    Community Expert
    Community Expert
    Jul 12, 2020 10:46 AM UTC

    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>

     

     

     

     

     

    Translate
    guccio1Author
    Participant
    Jul 13, 2020 2:23 PM UTC

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

    Translate
    Charlie Arehart
    Community Expert
    Community Expert
    Jun 18, 2020 5:38 PM UTC

    Yes, you can use the "request" scope.

    Translate
    /Charlie (troubleshooter, carehart. org)