Automatic data handler functions for partials in CFWheels 1.1

October 5, 2010 · Chris Peters

Just over a year ago, I blogged about my implementation of the RSS aggregator for cfwheels.org using Yahoo! Pipes, FeedBurner, <cffeed>, and CFWheels partials. Now that CFWheels version 1.1 is in beta (and almost complete), there is a new piece of functionality involving partials that makes my solution simpler.

Just over a year ago, I blogged about my implementation of the RSS aggregator for cfwheels.org using Yahoo! Pipes, FeedBurner, <cffeed>, and CFWheels partials. Now that CFWheels version 1.1 is in beta (and almost complete), there is a new piece of functionality involving partials that makes my solution simpler.

In my previous solution, I had to work around how controllers and partials interacted so that I could cache the contents of the partial:

The partial at views/community/_articles.cfm calls a view helper to get the feed data and displays the data. I had to use a view helper to link the partial back to the data that we needed. This was done so the caching could be controlled at the partial level.

Then my code had to contain this ugly function call at the top of the partial, which would feel much better if it were implemented in the controller instead:

<!--- Note: we are breaking the convention of data access in the view here so that we can cache this partial --->
<cfset articles = articles(arguments.articlesRss)>
view raw _articles.cfm hosted with ❤ by GitHub

Now CFWheels 1.1 offers the ability for you to implement a method in the controller that will get called automatically by a corresponding partial. The method must have private access, have a return type of struct, and be the same name as the partial.

So in the previous example, all I would need to do is have an articles() method with this declaration in my controller (or even the base controllers/Controller.cfc if I needed to use it across multiple controllers):

private struct function articles() {
// Logic goes here
}
view raw Controller.cfc hosted with ❤ by GitHub

Whatever keys get added to the struct that articles() returns becomes available as keys in the arguments struct passed to the partial. This works especially nice in this scenario because when I ask CFWheels to cache the partial, it doesn’t call the articles() method in the controller when the partial is cached.

With this new functionality, I was able to rewrite my code in a much cleaner, conventions-based approach while still using caching and not being banned from FeedBurner’s servers. Another big win for CFWheels!

About Chris Peters

With over 20 years of experience, I help plan, execute, and optimize digital experiences.

Leave a comment