Coldfusion 8 release added support for attributeCollection in Coldfusion tags. This feature makes it much more easy to wrap CF Tags as function and use them inside cfscript.
Now I am sure most Coldfusion users must already have created wrapper functions for CF tags ( similar to this ). One problem with this approach is that either you use only few attributes which the function accepts or write complex logic to check what attributes have been passed to function and use them. This makes it a little difficult to make use of all the functionality which a tag provides.
Now with attributeCollection all these issues are solved. You can use same wrapper function for a given tag and make use of all different functionality which that tag provides (ex. use cfzip function to list all entries of a zip file or create new file or delete one) . All that you need to do is write a wrapper function (for a given tag) which accepts a struct as argument. Pass this struct as attributeCollection to the tag, and that’s it!!, You are done. To use this inside cfscript, create a struct for all the attributes you want to pass this tag. Call the wrapper function and pass this struct.
Here’s sample code to show this.
<cfscript>
args.from=”#fromemail#”;
args.subject=”#subject#”;
args.server=”#mailserver#”;
args.port=”#port#”;
for(i=1;i<ArrayLen(tolist);i++)
{
args.to=”#toList[i]#”; //change email address of to field
args.subject=”Hello #tolist[i]#,” & args.subject; // personalize subject for each user
cfmail(args); //call function with changed arguments
}
</cfscript>
Wrapper Function for cfmail tag
<cffunction name=”cfmail” output=”no”>
<cfargument name=”arguments” type=”struct” required=”yes”>
<cfmail attributecollection=”#arguments#” >
</cfmail>
</cffunction>
As you see in above code sample, using attributeCollection code required to wrap CF tag inside function is reduced to a line now.
Note: Since attributeCollection support in Tags was added in Coldfusion8, above code will only work on Coldfusion8 and not on previous versions.