5

Inside the cfm of the cfmodule, values are returned through the use of Caller scope. If I call a cfmodule inside a function in a CFC, Caller maps to the Variables scope of the CFC correct? Can I return the values into to local scope of the CFC function?

Thanks

James A Mohler
  • 11,060
  • 15
  • 46
  • 72
Henry
  • 32,689
  • 19
  • 120
  • 221

1 Answers1

8

Yes, to all of the above. A demonstration:

Testing.cfc:

<cfcomponent>

    <cfset Variables.Instance = {} />

    <cffunction name="checkTheScopeYo" returntype="Struct">

        <cfset var LOCAL = {} />

        <!--- Call a CFModule --->
        <cfmodule template="TestModule.cfm" />

        <cfset Variables.theLocal = LOCAL />

        <cfreturn Variables />

    </cffunction>
</cfcomponent>

TestModule.cfm:

<cfif thisTag.ExecutionMode EQ "end">

    <cfset Caller.FromModule = "Set to the Variables scope" />

    <cfset Caller.Instance.FromModule = "Set to the Variables.instance variable" />

    <cfset Caller.Local.FromModule = "Set to the LOCAL scope" />

</cfif>

Scribble.cfm:

<cfset theResult = CreateObject("component", "Testing").checkTheScopeYo() />

<cfdump var="#theResult#">

The dump shows you that you have access to the local variables within the function, as well as the variables scope of the entire CFC:

struct

CHECKTHESCOPEYO:  
    [function]
    Arguments: none 
    ReturnType: Struct 
    Roles:  
    Access: public 
    Output:   
    DisplayName:  
    Hint:  
    Description:  
FROMMODULE: Set to the Variables scope
INSTANCE:  
    [struct]
    FROMMODULE: Set to the Variables.instance variable
THELOCAL:  
    [struct]
    FROMMODULE: Set to the LOCAL scope
THIS:  
    [component Testing]
    Methods: 
        CHECKTHESCOPEYO
            [function]
            Arguments: none 
            ReturnType: Struct 
            Roles:  
            Access: public 
            Output:   
            DisplayName:  
            Hint:  
            Description:  
Dan Short
  • 9,598
  • 2
  • 28
  • 53
  • so would the best practice be first var it like `var q = ""` and use ``? – Henry Sep 14 '11 at 00:39
  • Honestly, I have _never_ used a custom tag inside a CFC function. And I have on *very* rare occasions used the `Caller` scope. I rely on CustomTags for display... so if I need the output from a CustomTag for something, I wrap it in a `cfsavecontent` and get the HTML that way... If you must go this way though, yes, I would set a return variable name in an Attribute and set it to `Caller[Attributes.return]` from the CustomTag so you get the result where you want it without relying on a hard-coded `Caller` variable name. – Dan Short Sep 14 '11 at 00:43
  • Let me know if you want some sample code that would make that happen and I'll update the answer. Did I mention I love CustomTags? – Dan Short Sep 14 '11 at 00:48
  • Yes yes... the legacy system uses it EXTENSIVELY (pre-CFC era) and... no, I don't love it. :) – Henry Sep 14 '11 at 00:55
  • 1
    You must learn to love Custom Tags, but for the right reason :). And have something soft to throw at people when you have to work on that legacy system with custom tags in CFCs. – Dan Short Sep 14 '11 at 01:17
  • so does it mean, caller scope is not simply the variables scope of the CFC, but a mix of Local and Variables? – Henry Sep 14 '11 at 01:26
  • 1
    `Caller` scope does indeed give you access to everything that you would have in the call function, which means you get access both to the local function scope as well as the `Variable`s scope. I assume (and haven't tested yet) that it would follow the same scope chain rules as every other CF page. So if you had (in CF9 speak) a `LOCAL.foo` variable, as well as a `Variables.foo` variable, that the it would hit the `LOCAL` scope first, and then `Variables` scope. My sample doesn't address that situation. Seems like I need to do some more `cfset`s in there. – Dan Short Sep 14 '11 at 01:30
  • 1
    Act as if the module code is running inside the calling function, and that will inform what variables (and in what order) it will have access to them. – Dan Short Sep 14 '11 at 01:32
  • 1
    ok, so same behaviour as `` I suppose. Thanks! – Henry Sep 14 '11 at 01:36