Archive

Posts Tagged ‘udf’

Extending Coldfusion with UDFs

December 3rd, 2008

I cobbled together a few functions you may find useful this morning. As you can see they are simple, albeit useful, and so I wanted to share them here. Enjoy, and keep on coding!

/*
FUNCTION:   ArrayFindString
PURPOSE:    Searches inside the specified array (array_obj)
              for a string matching string (str)
PARAMETERS: 1. array_obj: the array containing the strings you
                 want to try to match to str
            2. str: the string you will be looking for in array_obj
RETURNS:    boolean
NOTE:       not case sensitive
*/
function ArrayFindString(array_obj, str){
var i = 1;

for (i = 1; i lte arrayLen(array_obj); i = i + 1)
if (array_obj[i] eq str) return true;

return false;
}

/*
FUNCTION: 	ArrayContainsString
PURPOSE: 	Searches inside the specified array (array_obj) for a
                  string that contains the string (str)
PARAMETERS: 	1. array_obj: the array containing the strings you want
                     to try to match to str
                2. str: the string you will be looking for in array_obj
RETURNS: 	boolean
NOTE: 		not case sensitive
*/
function ArrayContainsString(array_obj, str){
var i = 1;

for (i = 1; i lte arrayLen(array_obj); i = i + 1)
if (findNoCase(str, array_obj[i])) return true;

return false;
}

/*
FUNCTION: 	StringSearchArray
PURPOSE: 	Searches inside the specified string (str) for any
string matches contained in the array (array_obj)
PARAMETERS: 	1. str: the string you will be searching inside of
                2. array_obj: the array containing the strings you want
                     to look for in str
RETURNS: 	boolean
NOTE: 		not case sensitive
*/
function StringSearchArray(str, array_obj){
var i = 1;

for (i = 1; i lte arrayLen(array_obj); i = i + 1)
if (findNoCase(array_obj[i], str)) return true;

return false;
}

If your curious, I use StringSearchArray to control which areas of my application require user login:

...
foldersNoLogin = arrayNew(1);
foldersNoLogin[1] = "/login";
foldersNoLogin[2] = "/registration";
</cfscript>
<cfif not isDefined("session.loggedin") or session.loggedin eq false>
<cfif not StringSearchArray(cgi.path_info, foldersNoLogin)>
	<cflocation addtoken="no" url="/login/">
</cfif>
</cfif>

Free Stuff , , ,

Declaring UDFs in custom tags

November 11th, 2008

The title of this post may suggest to the reader that I am writing about something everyone at least moderately familiar with Cold Fusion should know. But, for the unaware, Cold Fusion compiles cfscript first, then it compiles the rest of the cfml. With this in mind, know that if you have a custom tag which has a closing tag, you are in fact calling your tag twice. This is where an ordinarily functional tag will have problems with udf’s declared in cfscript. Cold Fusion is not evaluating whether the tag is running in thistag.executionmode start or end at the time the cfscript is compiled. When this happens, the compiler will gripe about not being able to declare udf’s more than once.

I usually address this issue one of two ways:

  1. If the UDF could be used more than once I’d add it to a global repository of UDFs and load it into the server scope. More on that in a later post.
  2. If the UDF is specific enough to the custom tag being declared to not warrant adding it to the global repository I create a routines.inc file, declare the function there, and use cfinclude to include it from the custom tag. The compiler doesn’t gripe about declaring the tag more than once, because it is no longer in a cfscript block in the custom tag.

 

Final Note
If the custom tag does not have a close tag, you can still declare the function in a cfscript block within the tag. I, however, still prefer to use a separate file. I usually place my custom tags in their own folders to keep everything neet, but I’ll have more on how I organize my tags and functions in a later post. Until then, dream in digital.

Quick Tips , ,