<!--- 
 Filename: TextToTitleCase.cfm
 Author: Nate Weiss (NMW)
 Purpose: Creates the <cf_TextToTitleCase> custom tag
--->

<!--- Tag Parameters --->
<cfparam name="ATTRIBUTES.input" type="string">
<cfparam name="ATTRIBUTES.output" type="variableName">
<cfparam name="ATTRIBUTES.dontCapitalizeList" type="string"
 default="a,an,the,to,for,of">

<!--- Local Variables --->
<cfset result = "">

<!--- For each word in the input --->
<cfloop list="#ATTRIBUTES.input#" index="thisWord" delimiters=" ">

 <!--- Assuming this is a word that should be capitalized --->
 <cfif listFindNoCase(ATTRIBUTES.dontCapitalizeList, thisWord) eq 0>

   <!--- Grab the first letter, and convert it to uppercase --->
   <cfset firstLetter = uCase( mid(thisWord, 1, 1) )>
   <!--- Grab remaining letters, convert them to lowercase --->
   <cfset restOfWord = lCase( mid(thisWord, 2, len(thisWord)-1) )>
   
   <!--- Append the completed, capitalized word to result --->
   <cfset result = listAppend(result, firstLetter & restOfWord, " ")>

 <!--- If this is a word that should *not* be capitalized ---> 
 <cfelse>

   <cfset result = listAppend(result, thisWord, " ")>
 
 </cfif>

</cfloop>

<!--- Return result to calling template --->
<cfset "CALLER.#ATTRIBUTES.output#" = result>
