Search This Blog

Thursday, April 19, 2012

ColdFusion: argumentCollection vs. attributeCollection

Passing arguments/attributes via collection is very handy. However, you might get an unexpected error. This error could be caused by using argumentCollection or attributeCollection incorrectly.
See the code below and read the comments. I hope that helps! :)

<!--- Define a function to accept string and return it back to the caller --->
<cffunction name="echo" returntype="string" output="false">
 <cfargument name="aString" type="string" required="false" default="" />
 
 <cfreturn toString(arguments.aString) /> 
</cffunction>

<!--- Create a structure to hold an argument that will be passed to echo() --->
<cfset args_echo = structNew() />
<cfset args_echo.aString = "Hello World!" />

<!--- Call echo() and pass the argument via argumentCollection --->
<!--- NOTE: Notice that the argument name is "argumentCollection", instead of attributeCollection.
 In other words, if it is a function/method then use argumentCollection. --->
<cfoutput>#echo(argumentCollection=args_echo)#</cfoutput>


<!--- Create a structure implicitly (require CF8 and up); and declare attribute name and its value --->
<cfset args = {var="#args_echo#", label="args"} />

<!--- Call <cfdump> and pass the attributes via attributeCollection --->
<!--- NOTE: Notice that the attribute name is "attributeCollection", instead of argumentCollection.
 In other words, if it is a tag then use attributeCollection. --->
<cfdump attributeCollection="#args#" />

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.