Improving the tutorial: object- and query-driven partials in CFWheels

August 14, 2009 · Chris Peters

I mention in the CFWheels "Hello Database" tutorial that there are some things that could be factored out to make the code leaner. One thing that I would do is use a query-driven partial to factor out a loop that displays all users in the database.

I mention in the CFWheels Hello Database tutorial that there are some things that could be factored out to make the code leaner. One thing that I would do is use a query-driven partial to factor out a loop that displays all users in the database.

Writing a beginner tutorial like the CFWheels 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 CFWheels 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>
view raw index-old.cfm hosted with ❤ by GitHub

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>
view raw index.cfm hosted with ❤ by GitHub

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>
view raw _user.cfm hosted with ❤ by GitHub

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, wouldn’t it?)

About Chris Peters

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

Leave a comment