If you write a subroutine in any other language, you expect the calling program to be able to specify the variables where the returned information should be stored.
Unfortunately Allaire's documentation doesn't tell us how to do this for custom tags. If you write custom tags as illustrated in their docs, your tags will all have fixed output variable names. This has some disadvantages:
* risk of variable name conflicts.
* need to remember the output variable name(s) for each custom tag.
* you can't call the same subroutine several times with different arguments before using the result(s) of the first call.
I've just stumbled on the solution - the SetVariable function can create variables of Caller scope and varying names from within the called module. Here's a simple example:
In MODULE_1.CFM:
<cfmodule template="Module_2.cfm" RetVar="myVar">
<cfoutput><p>#myVar#</p></cfoutput>
In MODULE_2.CFM:
<cfset TempVar = SetVariable("Caller." & Attributes.RetVar, "Success!"
>
Run module_1.cfm and your page will show
Success!
Change both occurrences of myVar in MODULE_1.CFM to give:
<cfmodule template="Module_2.cfm" RetVar="anotherVar">
<cfoutput><p>#anotherVar#</p></cfoutput>
then run module_1.cfm again. Result:
Success!
Unfortunately Allaire's documentation doesn't tell us how to do this for custom tags. If you write custom tags as illustrated in their docs, your tags will all have fixed output variable names. This has some disadvantages:
* risk of variable name conflicts.
* need to remember the output variable name(s) for each custom tag.
* you can't call the same subroutine several times with different arguments before using the result(s) of the first call.
I've just stumbled on the solution - the SetVariable function can create variables of Caller scope and varying names from within the called module. Here's a simple example:
In MODULE_1.CFM:
<cfmodule template="Module_2.cfm" RetVar="myVar">
<cfoutput><p>#myVar#</p></cfoutput>
In MODULE_2.CFM:
<cfset TempVar = SetVariable("Caller." & Attributes.RetVar, "Success!"
Run module_1.cfm and your page will show
Success!
Change both occurrences of myVar in MODULE_1.CFM to give:
<cfmodule template="Module_2.cfm" RetVar="anotherVar">
<cfoutput><p>#anotherVar#</p></cfoutput>
then run module_1.cfm again. Result:
Success!