Helpers in the Model - Modelglue 3
Posted by Gabriel Landes | Tags: Coldfusion , Modelglue
MG3 introduced 'helpers' allowing you to define some UDF's in a file, save it to the 'helpers' folder and call them throughout your controller and view code using their new 'helpers' scope. Handy. I ran into a situation where I needed a particular function in my model and realized the 'helpers' scope was not to be found. Specifically, I have a function 'getText' that returns a string in the proper language for the user. Since, text is created in the model at times (error messages, etc.), I need this there as well as in the view. So, this was my solution.
Regex for Title Case
Posted by Gabriel Landes | Tags: Regex , Snippets
Here is a simple solution for title casing a string in Coldfusion using regex:
&lt;cfset string = "THIS 1 is one a+ title" /&gt;<br />
&lt;cfset convert = reReplace(lCase(string),'\b([a-z])','\U\1','all') /&gt;<br />
&lt;cfoutput&gt;#convert#&lt;/cfoutput&gt;
Here is what is going on. We start with a string of mixed case and characters which could include numbers and special characters. Working from the inside out of the 2nd line, we first convert the whole string to lower case with the 'lCase' function. Then we use 'reReplace' to replace any leading letters with their uppercase version.
The regular expression is "\b([a-z])". The \b is an anchor which finds the word boundaries - the beginning and end of each word. Since the boundary doesn't actually capture anything, we add the next part, "[a-z]", to capture any lowercase letter following a word boundary. The parentheses around the second part tell the engine to "remember" what was captured.
In the replace part of the reReplace function we put "\U\1". This does 2 things: "\U" calls for uppercase output of what comes next and "\1" refers to what was captured by the 1st (and only in this case) set of parenthesis. In short we capture the first letter after a word boundary and replace it with the uppercase version of itself.
To 'www' or not to 'www'
Posted by Gabriel Landes | Tags: Coldfusion , SEO
So right out of the gate on this new blog, I ran into a little hurdle that more than a few people ignore completely: how do I force traffic to my site to use (or not use) the 'www' at the beginning of the domain name. Most webservers will not force traffic either way unless you tell them to. But, before most people wonder "how," they wonder "why." So, let's start there.