Utility Function: ListIsEmpty()

While this is a simple bit of code to write, I personally like the syntax of checking "something is empty" over writing len(something) gt 0.

<cffunction name="ListIsEmpty" access="public" output="false" returntype="boolean">
    <cfargument name="list" type="string" required="true" />
    <cfargument name="delimiters" type="string" required="false" default="," />
    
    <cfreturn (ListLen(arguments.list, arguments.delimiters) gt 0) />
</cffunction>

 

Utility Function: ArrayFind()

I sometimes need to search an array of strings for a specific value, so I came up with this function.

<cffunction name="ArrayFind" access="public" output="false" returntype="numeric">
    <cfargument name="array" type="array" required="true" />
    <cfargument name="find" type="any" required="true" />
    
    <cfset var position = 0 />
    <cfset var x = 0 />
    
    <cfloop from="1" to="#ArrayLen(arguments.array)#" index="x">
        <cfif arguments.array[x] eq arguments.find>
            <cfset position = x />
            <cfbreak />
        </cfif>
    </cfloop>
    
    <cfreturn position />
</cffunction>

 

Utility Function: RenderTemplate()

This function is half-way to being a really nice function. I just wish that I could pass in arguments, or a structure to use as the variables available in the rendered template. I'd love to have that isolation...


<cffunction name="RenderTemplate" access="public" output="false" returnType="string">
    <cfargument name="path" type="string" required="false" />
    
    <cfset var local = StructNew() />
    
    <cfsavecontent variable="local.returnValue">
        <cfinclude template="#arguments.path#" />
    </cfsavecontent>
    
    <cfreturn local.returnValue />
</cffunction>

 

Utility Function: JSClean()

This one I use a lot when writing out JSON. It probably is missing something that will still break a JS string, but its all I've found so far.


<cffunction name="JSClean" access="public" returntype="string" output="false">
    <cfargument name="sString" type="string" required="true" />
        
    <cfset var sReturn = REReplaceNoCase(Trim(arguments.sString), "\s", " ", "ALL") />
        
    <cfloop list="\:':"":&" delimiters=":" index="x">
        <cfset sReturn = ReplaceNoCase(sReturn, x, "\#x#", "ALL") />
    </cfloop>
        
    <cfreturn sReturn />
</cffunction>

 

Utility Function: Redirect()

Another simple wrapper function, this one for <cflocation/>.


<cffunction name="Redirect" access="public" output="false" returntype="void">
    <cfargument name="url" type="string" required="true" />
    <cfargument name="addToken" type="boolean" required="false" default="false" />
        
    <cflocation url="#arguments.url#" addtoken="#arguments.addToken#" />
    <cfabort />
        
    <cfreturn />
</cffunction>

 

Utility Function: IncludeScript()

I use this one to dynamically include JS I need for a specific page. Its really just a specialized wrapper for <cfhtmlhead/> but I like it better.


<cffunction name="IncludeScript" access="public" output="false" returntype="void">
    <cfargument name="script" type="string" required="true" />
        
    <cfhtmlhead text="<script type=""text/javascript"" src=""#arguments.script#""></script>" />
        
    <cfreturn />
</cffunction>

 

More Entries

Jon Hartmann, July 2011

I'm Jon Hartmann and I'm a Javascript fanatic, UX/UI evangelist and former ColdFusion master. I blog about mysterious error messages, user interface design questions, and all things baffling and irksome about programming for the web.

Learn more about me on LinkedIn.