Posted in ColdFusion | Posted on 05-14-2007 | 2,084 views
One of the announced features for ColdFusion 8 is support for dynamically passing attributes to ColdFusion tags. This will be a godsend for tags like CFMAIL. A reader asked me (and by the way, folks, don't ask me about Scorpio, in general I can't answer) if this support carried over to cfmodule and custom tags.
This is actually something that has been supported in ColdFusion for quite some time. I don't have the exact version it was added to ColdFusion, but custom tags have had support for 'attributeCollection' for a while. Consider this simple custom tag:
If you first run it like so:
You will see a struct with 2 keys, name and age. However, you can get the same result like so:
2<cfset s.name = "ray">
3<cfset s.age = 34>
4<cf_customtag attributeCollection="#s#">
At the same time, ColdFusion UDFs support an argumentCollection value. Consider this example:
2 <cfreturn arguments>
3</cffunction>
4
5<cfset result = mirrorargs(name='ray',age=34)>
6<cfdump var="#result#">
This returns a structure much like the custom tag example. Now imagine we call it like so:
2<cfset s.name = "ray">
3<cfset s.age = 34>
4<cfset result=mirrorargs(argumentCollection=s)>
5<cfdump var="#result#" label="Result using argumentcollection">
This will return the same structure as well. (Although to be clear - the case of the structs keys is different, but you should not be relying on struct key case anyway.)


This is also true even if you do not use argumentCollection. You could pass arguments like ("argument1","argument2") and they will come though as an array of unnamed arguments if there are no cfargument tags. Lastly, you could do (argument1="argument1",argument2="argument2") and if there are no matching cfargument tags in the component, you will still get an arguments structure with the named keys.
Whether this is a good practice or not is an entirely seperate argument :)
To make matters even more confusing, you CAN treat arguments like an array too if you want.
Which you kind of say - but just adding.
http://www.cfinternals.org/blog/2007/05/named_argu...