Page Layout with Body IDs

HTML Code Snippet A while back I read about a technique to maximize the power of CSS: placing a unique ID on the <body> tag of your markup. Make each page have a unique body ID, and you can use CSS to override anything on that particular page if you want. I've played around with this a bit, and found it to be quite powerful technique.

 

Automatic Redirect to Secure Site

A combination lock Its not that this is really all that hard to do, but a common security issue is to make sure that users are going to a HTTPS version of a site. This code snippet handles automatic redirection of a user from the unsecured version of a page to the secured version.

<!--- See if HTTPS is off --->
<cfif cgi.https eq "off">
    <!--- Get site and path --->
    <cfset location = "https://" & cgi.http_host & cgi.script_name />
    
    <!--- See if there are any URL params --->
    <cfif len(cgi.query_string)>
        <!--- Append the URL params --->
        <cfset location = location & "?" & cgi.query_string />
    </cfif>
    
    <!--- Redirect to the secure verion --->
    <cflocation url="#location#" />
</cfif>

As a side effect, it also blocks POSTs to the unsecured version. I'm not sure if thats a feature or a bug though.

 

CF on Wheels: What I Like

CF on Wheels LogoOver the last few days, I've taken the time to make myself a little more familiar with CF on Wheels, and I've I'm really digging it. CF on Wheels bills itself as a ColdFusion framework "in the spirit of Ruby On Rails," and I think it lives up to the tag line. Click "more" to read more about it.

 

Utility Function: StructDeleteKeys()

A gear I often need to delete struct keys based on a list of items. Its not that I can't just loop the list and call StructDelete(), but it gets old, and this function makes it a little easier.

<cffunction name="StructDeleteKeys" returntype="void">
    <cfargument name="struct" type="struct" requried="true" />
    <cfargument name="keyList" type="string" requried="true" />
    <cfargument name="delimiters" type="string" required="false" default="," />
    
    <cfloop list="#arguments.keyList#" delimiters="#arguments.delimiters#" index="key">
        <cfset StructDelete(arguments.struct, key) />
    </cfloop>
    
    <cfreturn />
</cffunction>

 

Utility Functions: StructToListPairs() and ListPairsToStruct()

Today we have a pair of functions designed to convert structures into list pairs and back again. They serve as the back bone of another pair of functions that I'll release soon. StructToListPairs() is also useful when constructing long links (such as creating "sort" links for tables).

<cffunction name="StructToListPairs" returntype="string">
    <cfargument name="struct" type="struct" requried="true" />
    <cfargument name="delimiter1" type="string" required="false" default="&" />
    <cfargument name="delimiter2" type="string" required="false" default="=" />
    
    <cfset var buffer = CreateObject("java", "java.lang.StringBuffer").init() />
    
    <cfloop collection="#arguments.struct#" item="key">
        <cfset buffer.append(key) />
        <cfset buffer.append(delimiter2) />
        <cfset buffer.append(arguments.struct[key]) />
        <cfset buffer.append(delimiter1) />
    </cfloop>
    
    <cfif buffer.length() gt 1>
        <cfset buffer.setLength(buffer.length()-1) />
    </cfif>
    
    <cfreturn buffer.toString() />
</cffunction>

<cffunction name="ListPairsToStruct" returntype="struct">
    <cfargument name="list" type="string" requried="true" />
    <cfargument name="delimiter1" type="string" required="false" default="&" />
    <cfargument name="delimiter2" type="string" required="false" default="=" />
    
    <cfset var returnValue = StructNew() />
    
    <cfloop list="#arguments.list#" index="pair" delimiters="#arguments.delimiter1#">
        <cfset returnValue[ListFirst(pair, arguments.delimiter2)] = ListLast(pair, arguments.delimiter2) />
    </cfloop>
    
    <cfreturn returnValue />
</cffunction>

 

Utility Functions: Three String Functions

Here is a trio of functions based on a question from Hal Helm's "Are you OO Ready" quiz, in which he mentions that you can do the following:

<cfset x = "something" />
<cfset y = x />
<cfoutput>#y.equals(x)#</cfoutput>

So I came up with these:

<cffunction name="StringToChars" access="public" output="false" returntype="array">
    <cfargument name="string" type="string" required="true" />
    
    <cfreturn arguments.string.toCharArray() />
</cffunction>

<cffunction name="StringEndsWith" access="public" output="false" returntype="boolean">
    <cfargument name="string" type="string" required="true" />
    <cfargument name="suffix" type="string" required="true" />
    
    <cfreturn arguments.string.endsWith(arguments.suffix) />
</cffunction>

<cffunction name="StringBeginsWith" access="public" output="false" returntype="boolean">
    <cfargument name="string" type="string" required="true" />
    <cfargument name="prefix" type="string" required="true" />
    
    <cfreturn arguments.string.startsWith(arguments.prefix) />
</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.