Best Of ColdFusion 10 Contest Entry - HTML Email Utility

Posted May 1, 2012 at 9:45 AM by Ben Nadel

Tags: ColdFusion

As you know, Ray Camden has been hosting the latest "Best Of ColdFusion" contest for the public beta of ColdFusion 10. The point of the contest is to have fun, naturally, but also to showcase one or more of the features added in the latest release of the language. For my entry, I created a single-page application that will take an HTML page with STYLE tags and output new HTML with the CSS properties merged into the HTML document as Style attributes.


 
 
 

 
Best of ColdFusion 10 Contest Entry by Ben Nadel. 
 
 
 

This project is available on my GitHub account.

According to the Campaign Monitor CSS for Email guide, Style tags will be stripped out of Google Mail emails. And, since Google Mail is the email client that I use (and is apparently one of the most limited email clients), I thought it would be fun to create a utility that would merge easy-to-read/maintain Style tags into the Google-Mail-compliant inline Style attribute. This way, you could develop HTML emails like you would any other HTML page; then, "compile" the email version as a subsequent build step.

NOTE: Even the HTML Email Boilerplate advocates moving most of your CSS styles inline.

In the past, I've tried to do this by converting CSS selectors into XPath queries and applying them to a strict XHTML (ie. XML) document. Now that ColdFusion 10 supports the loading of per-application Java libraries, however, I thought it would be fun to have Java do some of the heavy lifting for me.

I decided to use the jSoup library as the means to parse the incoming HTML, extract the Style tags, and then find the target elements based on the given CSS selectors. The loading of Java libraries was the big feature that I was trying to explore; but, in the end, I touched upon several new features of ColdFusion 10:

  • Per-Application Java Loading: As I stated above, I'm using the per-application JAR settings to load the jSoup library.
  • Invoke Implicit Accessors: This settings allows property access to implicitly invoke the associated getters/setters without an explicit method call. I am not sure how I feel about this one. I used it (CSSRule.cfc) mostly because I have never used it before - not because I thought that it was necessarily the right tool for the job. I am a little confused as to the proper use-case for it.
  • arrayAppend( x, y, true ): I absolutely love the new, third argument for arrayAppend() which allows the appended value to be flattened into the target array. This is hugely useful.
  • arrayEach(): Using closures to iterate over an array. Awesome! Though, I must say that using For-In on an array feels equally useful at times. But, there are times when arrayEach() feels like the better choice.
  • arraySort(): Using closures to define the comparator for the sort.
  • structEach(): Using closures to iterate over the key-value pairs.

In addition to any ColdFusion 10 specific feature, I also wrote this application (the server-side aspects) entirely in CFScript. As I've stated before, ColdFusion 10 feels like it facilitates CFScript much more than any of its predecessors, perhaps primarily because it supports Closures (which makes me think of JavaScript).

Anyway, this was a lot of fun to build; though, I must admit that my inadequate knowledge of Object Oriented Programming (OOP) is beginning to frustrate me in my ability to craft a beautiful domain model. I feel like so much more elegance could have been applied here! This is timely, however, as I really want to dedicate myself back to learning about application architecture.




Reader Comments

May 1, 2012 at 10:12 AM // reply »
11,246 Comments

@All,

I added a copy of this code to my Hostek Beta account:

http://bennadel-cf10beta.securecb1cf10.ezhostingserver.com/bestofcf10/


May 1, 2012 at 4:45 PM // reply »
1 Comments

Hey Ben, great stuff as always

Just playing with the styles and couldn't get the container class to change font size.

  • font-size 16px;

Missing colon? :)


May 1, 2012 at 9:50 PM // reply »
11,246 Comments

@Brendan,

Nice catch! The CSS parser just ignores properties that don't "make sense". I probably didn't notice cause I think 16px is the default font-size on the browser. Good catch!


May 16, 2012 at 8:18 PM // reply »
6 Comments

Just found this, looks good!

I'm trying to run it on local, it's the 64bit version and I'm experiencing horrible lag. On average the generate.cfm processes the content change in 60-90 seconds. I've tested it with 4 different browsers and even stopped service on CF9 multiserver instance.

Thoughts?

Thanks!
Jaana


Feb 6, 2013 at 3:02 PM // reply »
3 Comments

Looks awesome, but sadly, I'm stuck on CF 9.0.1.

Anyone aware of a comparible utility that will run on that version of CF?


Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
May 23, 2013 at 9:52 PM
Preventing Links In Standalone iPhone Applications From Opening In Mobile Safari
@Muhmmadibn Did you figure out a solution to launching PDFs? I am running into the same issues myself. There is no way to close the PDF or go back once you launch it. Thanks in advance! ... read »
May 23, 2013 at 6:06 PM
The Girl Who Broke My Heart, And Made Me A Better Person
Good day,ladies and gentle men, my name is Dr AMADI the great spell caster in Africa, i have help so many people for different kind of problems,who say there is no solution to problems on earth, that ... read »
May 23, 2013 at 4:26 PM
ColdFusion QueryAppend( qOne, qTwo )
@Heather, Glad people are still getting value out of this! ... read »
May 23, 2013 at 3:49 PM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@WebManWalking, I meant the code at the bottom (not the video). I did try to experiment with an intermediary variable, like: value = users.id[ i ]; arrayContains( userIDs, value ); ... but t ... read »
May 23, 2013 at 11:06 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@Ben, Are you talking about As Number: YES As String: YES As Java: YES? If so, that's with 3 different ways of referencing the constant 1, not users.id[1]. Query object references(*) are what seem ... read »
May 23, 2013 at 9:55 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@Dan, According to the CF Admin, I'm running Java "1.6.0_45". As far as the DB column, in the database it's an INT. I'll see if I can dig into what CF sees it as. @WebManWalking, But h ... read »
May 23, 2013 at 9:49 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@Ben, I think the problem is that we're used to loose typing in ColdFusion, like JavaScript. If a value is a number but it's needed in an expression to be a string, noooo problem. I've encountered ... read »
May 23, 2013 at 9:47 AM
ColdFusion QueryAppend( qOne, qTwo )
You rock! Thank you, thank you, thank you!!! ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools