"The Art of Creating Functions in ColdFusion" by  John Morgan
CFDJ 2-6 p.38
 

LISTING 1: BasicUDF.cfm
<!--- Surround the string passed via Param1 with asterisks --->
<CFSET Result = "*" & Attributes.PARAM1 & "*">
<!--- Create a variable named BasicUDF and assign it the result --->
<CFSET Caller.BasicUDF = Result>
 

LISTING 2: BasicUDF2.cfm
<!--- Localize Variables --->
<CFSET par_Param1 = Attributes.PARAM1>
<!--- Surround the string passed via Param1 with asterisks --->
<CFSET Result = "*" & par_Param1 & "*">
<!--- Create a variable named BasicUDF2 and assign it the result --->
<CFSET Caller.BasicUDF2 = Result>
 

LISTING 3: BasicUDF3.cfm
<!--- Get the value of the parameter that was passed in Caption --->
<CFSET par_StrVar = Evaluate("Caller.#Attributes.StrVarName#")>
<!--- Surround the string passed via Param1 with asterisks --->
<CFSET Result = "*" & par_StrVar & "*">
<!--- Assign the return value to the parameter that was passed --->
<CFSET "Caller.#Attributes.StrVarName#" = "#Result#">
 

LISTING 4: BasicUDF4.cfm
<CFIF IsDefined("Attributes.StrVarName")>
   <!--- Get the value of the parameter tha was passed in Caption --->
   <CFSET par_StrVar = Evaluate("Caller.#Attributes.StrVarName#")>
   <!--- Surround the string passed via Param1 with asterisks --->
   <CFSET Result = "*" & par_StrVar & "*">
   <!--- Assign the return value to the parameter that was passed --->
   <CFSET "Caller.#Attributes.StrVarName#" = "#Result#">
<CFELSE>
   <H3>ERROR: StrVarName is a required parameter for CF_BasicUDF4</H3>
   <CFABORT>
</CFIF>
 

LISTING 5: BasicUDF5.cfm
<CFIF IsDefined("Attributes.StrVarName")>
   <!--- Get the character that was passed in Symbol
      or default to "*" --->
   <CFPARAM NAME="Attributes.Symbol" DEFAULT="*">
   <CFSET par_Symbol = Attributes.Symbol>
   <!--- Get the value of the parameter tha was passed in Caption --->
   <CFSET par_StrVar = Evaluate("Caller.#Attributes.StrVarName#")>
   <!--- Surround the string passed via Param1 with asterisks --->
   <CFSET Result = par_Symbol & par_StrVar & par_Symbol>
   <!--- Assign the return value to the parameter that was passed --->
   <CFSET "Caller.#Attributes.StrVarName#" = "#Result#">
<CFELSE>
   <H3>ERROR: StrVarName is a required parameter for CF_BasicUDF5</H3>
   <CFABORT>
</CFIF>
 

LISTING 6: ProperBV.cfm
<!--- Initialize All Tag Options --->
<CFPARAM NAME="Attributes.String" DEFAULT="">
<CFSET par_String = Attributes.String>
 

<CFSET StrLength = Len(par_String)>
<!--- Bail out if the string is empty --->
<CFIF StrLength EQ 0>
   <CFEXIT>
</CFIF>
 

<!--- Force a space so the we capitalize the first letter --->
<CFSET LastChar = " ">
<CFSET Result = "">
 

<CFLOOP INDEX="ThisIndex" FROM="1" TO="#StrLength#">
   <!--- Get the character at the current index --->
   <CFSET ThisChar = Mid(par_String, ThisIndex, 1)>
   <CFIF LastChar EQ " ">
      <!--- If ThisChar follows a space then uppercase it --->
      <CFSET ThisChar = UCase(ThisChar)>
   <CFELSE>
      <!--- Otherwise lowercase it --->
      <CFSET ThisChar = LCase(ThisChar)>
   </CFIF>
   <!--- Append ThisChar to the Output string --->
   <CFSET Result = Result & ThisChar>
   <!--- Set LastChar to ThisChar for comparison --->
   <CFSET LastChar = ThisChar>
</CFLOOP>
 

<!--- This creates a variable named after the function --->
<CFSET Caller.Proper = #Result#>
 

LISTING 7: ProperBR.cfm
<!--- Initialize All Tag Options --->
<CFPARAM NAME="Attributes.StrVarName" DEFAULT="">
 

<!--- Store the VALUE of the variable passed --->
<CFSET lString = Evaluate("Caller.#Attributes.StrVarName#")>
 

<CFSET StrLength = Len(lString)>
<!--- Bail out if the string is empty --->
<CFIF StrLength EQ 0>
   <CFEXIT>
</CFIF>
 

<!--- Force a space so the we capitalize the first letter --->
<CFSET LastChar = " ">
<CFSET Result = "">
<CFLOOP INDEX="ThisIndex" FROM="1" TO="#StrLength#">
   <!--- Get the character at the current index --->
   <CFSET ThisChar = Mid(lString, ThisIndex, 1)>
   <CFIF LastChar EQ " ">
      <!--- If ThisChar follows a space then uppercase it --->
      <CFSET ThisChar = UCase(ThisChar)>
        <CFELSE>
      <!--- Otherwise lowercase it --->
      <CFSET ThisChar = LCase(ThisChar)>
   </CFIF>
   <!--- Append ThisChar to the Output string --->
   <CFSET Result = Result & ThisChar>
   <!--- Set LastChar to ThisChar for comparison --->
   <CFSET LastChar = ThisChar>
</CFLOOP>
 

<!--- Set the VALUE into the variable passed --->
<CFSET "Caller.#Attributes.StrVarName#" = Result>
 

LISTING 8: ProperTest.cfm
Before ProperBV and ProperBR
TEST1 = #Test1#
TEST2 = #Test2#
PROPER = #Proper#
Proper is Undefined

--------------------------------------------------------------------------------
After ProperBV
TEST1 = #Test1#
TEST2 = #Test2#
PROPER = #Proper#
Proper is Undefined

--------------------------------------------------------------------------------
After ProperBR
TEST1 = #Test1#
TEST2 = #Test2#
PROPER = #Proper#
Proper is Undefined