Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to make Custom Tag return caller-specified variable 1

Status
Not open for further replies.

philcha

Programmer
May 10, 2000
109
GB
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=&quot;Module_2.cfm&quot; RetVar=&quot;myVar&quot;>
<cfoutput><p>#myVar#</p></cfoutput>

In MODULE_2.CFM:
<cfset TempVar = SetVariable(&quot;Caller.&quot; & Attributes.RetVar, &quot;Success!&quot;)>

Run module_1.cfm and your page will show
Success!

Change both occurrences of myVar in MODULE_1.CFM to give:
<cfmodule template=&quot;Module_2.cfm&quot; RetVar=&quot;anotherVar&quot;>
<cfoutput><p>#anotherVar#</p></cfoutput>

then run module_1.cfm again. Result:
Success!
 
Hello philcha,

There is an even short method:

In MODULE_2.CFM:
<CFSET &quot;Caller.#Attributes.RetVar#&quot; = &quot;Success!&quot;>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top