Reputation: 491
I'm a bit new to ColdFusion (coming from Java/OOP world)
I have a custom tag that runs some things inside a cfscript and outputs a value. I'd like to have this custom tag (let's call it A) call another custom tag (let's call it B, a more general custom tag that has a sort of 'static' function) with a certain parameter.
How do I call B from within A? How do I use the return value from B in A?
A's code
<cfscript>
//Call to other custom tag here?:
//foo = [CUSTOMTAG param="stuff"];
value = foo;
</cfscript>
<cfoutput>#value#</cfoutput>
Upvotes: 1
Views: 835
Reputation: 491
Here's what I ended up doing. In my "A" file (recall, A calls B):
<cfscript>
b = createObject("component","bName");
returnVal = b.method("paramInfo");
</cfscript>
In my "B" file
<cfcomponent displayname="bName">
<cffunction name="method" returntype="string" output="false">
<cfargument name="paramName" required="yes" type="string">
<cfscript>
returnVal = paramName;
</cfscript>
<cfreturn returnVal>
</cffunction>
</cfcomponent>
Upvotes: 1
Reputation: 32905
Are you maintaining some old code? Otherwise, writing a User-defined function would be way easier for you to use and/or to test than using an old-school custom tag.
While custom tag is good at doing things like generating markups, it's not that good at returning variable.
You should be looking into writing user-defined function (UDF) instead, just like how you'd do in Java.
Not saying custom tag can't return value(s), it is just not as natural as function.
To return values from a custom tag, you need to make use of Caller
scope (which maps to the variables
scope of the caller). Something like...
<!--- customTag.cfm --->
<cfset caller[attributes.returnVar] = "some value">
And call the customTag like this:
<cf_customTag returnVar="foo">
<cfdump var="#variables.foo#">
Just keep track of who's the caller, and set the value in the caller scope, and you can invoke custom tag inside a custom tag, inside a custom tag, inside a custom tag...
If you need to capture the output of a custom tag into a var, wrap the custom tag call inside a <cfsavecontent>
</cfsavecontent>
.
Upvotes: 6