Can cfmodule return values to caller's local scope?
Asked Answered
L

1

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

Lacrimator answered 13/9, 2011 at 23:47 Comment(0)
U
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:  
Unconsidered answered 14/9, 2011 at 0:22 Comment(9)
so would the best practice be first var it like var q = "" and use <customTag return="q">?Lacrimator
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.Unconsidered
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?Unconsidered
Yes yes... the legacy system uses it EXTENSIVELY (pre-CFC era) and... no, I don't love it. :)Lacrimator
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.Unconsidered
so does it mean, caller scope is not simply the variables scope of the CFC, but a mix of Local and Variables?Lacrimator
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 Variables 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 cfsets in there.Unconsidered
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.Unconsidered
ok, so same behaviour as <cfquery name="q"> I suppose. Thanks!Lacrimator

© 2022 - 2024 — McMap. All rights reserved.