6 Replies Latest reply on Nov 30, 2006 2:38 PM by Homestar9

    Accessing CFMODULE variable scope

    Homestar9 Level 1
      If you have a ColdFusion template which calls another template within a CFMODULE tag, can you later access variables set in the CFMODULE tag from the caller template? I am aware of the CALLER scope within CFMODULES however, I was wondering if there is a scope to go the other direction so templates can get the variables within the cfmodule?

      For example:

      ---SecondPage.cfm---

      <CFSET Apples="yes">

      ---End---

      ---FirstPage.cfm---

      <CFMODULE template="secondpage.cfm">

      The value for apples is: <CFOUTPUT>#[scope].Apples#</CFOUTPUT>

      ---End---

        • 1. Re: Accessing CFMODULE variable scope
          Level 7
          > caller template? I am aware of the CALLER scope within CFMODULES however, I
          > was wondering if there is a scope to go the other direction so templates can
          > get the variables within the cfmodule?

          <!--- in module template --->
          <cfset caller.apples = "yes">

          <!--- in calling template --->
          <cfoutput>#variables.apples#</cfoutput>

          http://livedocs.macromedia.com/coldfusion/7/htmldocs/00001074.htm

          --
          Adam
          • 2. Re: Accessing CFMODULE variable scope
            Homestar9 Level 1
            Thanks for the reply Adam.

            I am aware of the CALLER scope to send data from the module back to the calling template. However, what I want is to be able to reference variables within the module from within the calling template.

            It seems like if we could name the instance of the module, we should be able to reference these variables using the named instance before the variable name. like #MyModuleName.VariableName#.

            Perhaps there is no way to do this in the current verion of Coldfusion, or perhaps there is a variable scope I am unaware of?
            • 3. Re: Accessing CFMODULE variable scope
              Level 7
              I understood what you asked.

              Did you read the docs I sent you?

              The closest thing to what you want is to put the values in the caller
              scope. That said, you should really NOT want to be doing what you're doing
              anyhow, as the calling code should not "interested" in the vagaries of what
              goes on in the module. It's te whole idea of using modules...
              abstracting/hiding processing from the calling code.

              What are you trying to achieve?

              --
              Adam
              • 4. Re: Accessing CFMODULE variable scope
                Homestar9 Level 1
                I am trying to create CFMODULE pages that may be called from either a CFMODULE tag or a CFINCLUDE. There are some cases where I want to protect variable names and others where it is not as important.

                I was hoping to eliminate the need for setting variables to the CALLER scope within the cfmodule. If there would be a way for me to access the variables in the module from the calling template it would give me more flexibility.

                Perhaps the best way to do this is to have the module page check to see if indeed it is a module first and then through a series of CFIF statements set variables normally or via the caller scope.
                • 5. Re: Accessing CFMODULE variable scope
                  Level 7
                  > I was hoping to eliminate the need for setting variables to the CALLER scope
                  > within the cfmodule.

                  Given this is the way to achieve what you want to do, why do you want to
                  "eliminate" it?

                  There is no "special scope". All the local memory from the module is
                  released when it finishes. To effect what you want to do, you need to set
                  values in a scope other than the local memory scope of the module. The
                  most appropriate is in the variables scope of the calling code, which you
                  do via the caller scope. That's it. That's the answer.

                  If you think it's NOT the answer, it would really help your cause if you
                  could explain *what you're actually trying to achieve*, and why you don't
                  want to use the caller scope.

                  --
                  Adam
                  • 6. Re: Accessing CFMODULE variable scope
                    Homestar9 Level 1
                    Bottom Line: There is no way to access variables that are set in a CFMODULE. You must use the CALLER scope to transfer variable values to the calling template.

                    Thanks Adam. Have a good holiday.