Improving the tutorial: Object- and query-driven partials in ColdFusion on Wheels
Friday, August 14th, 2009Writing a beginner tutorial like the Wheels Hello Database tutorial is tough. There are so many things that you want to do to improve the code. But you also don’t want for the tutorial to be 20 pages long. So you kind of need to let go and leave things where they are.
That said, I have a blog where I can say anything that I want to.
Here is one way that I would improve the Hello Database tutorial: through the use of a query-driven partial. I don’t even think that query-driven partials were available in Wheels when I wrote the tutorial, so this blog post can be a double-win.
In the tutorial, we could factor out a few lines into a partial and end up with a little bit less code in our view. Here’s how the table listing all users is written in the tutorial:
<table> <thead> <tr> <th>Name</th> <th>Email</th> <th colspan="2"></th> </tr> </thead> <tbody> <cfloop query="users"> <tr> <td>#users.name#</td> <td>#users.email#</td> <td>#linkTo(text="Edit", action="edit", key=users.id, title="Edit #users.name#")#</td> <td>#linkTo(text="Delete", action="delete", key=users.id, title="Delete #users.name#", confirm="Are you sure that you want to delete #users.name#?")#</td> </tr> </cfloop> </tbody> </table>
As it turns out, the whole <cfloop> block can be factored out into a partial. But it won’t be a “normal” partial. Instead of passing in a name of a partial to the includePartial() function, we’ll pass the query object itself.
So now we can replace the loop and its contents with this line:
<table> <thead> <tr> <th>Name</th> <th>Email</th> <th colspan="2"></th> </tr> </thead> <tbody> #includePartial(users)# </tbody> </table>
Much tidier. No mixture of CFML and HTML tags.
Because the query object is named users, Wheels will look for a partial called _user.cfm automatically. In my opinion, this makes naming of the partials very straightforward and almost self-documenting.
Now that the view file is a little cleaner, we put our table row in views/users/_user.cfm. This becomes the contents of the partial:
<cfoutput> <tr> <td>#arguments.name#</td> <td>#arguments.email#</td> <td>#linkTo(text="Edit", action="edit", key=users.id, title="Edit #arguments.name#")#</td> <td>#linkTo(text="Delete", action="delete", key=users.id, title="Delete #arguments.name#", confirm="Are you sure that you want to delete #arguments.name#?")#</td> </tr> </cfoutput>
As you can see, the properties of the query object become available in the arguments scope. And the partial gets called automatically for each iteration through the record set.
This same partial would also work if we passed includePartial() a single user model object as well. Pretty powerful, reusable stuff.
There’s at least one other thing that I would do to improve the tutorial, so stick around and see in a future post. (That would make this a good time to subscribe by RSS or email, wouldn’t it?)



September 4th, 2009 at 9:32 am
Hi, I really like your beginners’ tutorials. In the Hello Database tutorial (with version 0.9.4 of Wheels) I get the “onErrors method not found” error when following your steps. Ultimiately I just ended up commenting out line 1035 of Wheels’ forms.cfm just to move on… but thought I’d mention it. Thanks
September 6th, 2009 at 12:04 am
Thanks for the compliment, Stephen. I’ll make sure to go through the tutorials before we release 0.9.4 (which should be sometime next week).