BlogCFC Tweaks: View Entries By Category

With BlogCFC, there isn't an easy way to browse your entries by category. The closest you can get is to go to Entries put the category into the search. This actually wasn't too hard, once I figured out some of the more cryptic bits of some of the custom tags in BlogCFC.

Warning, this code is down and dirty. It works for me, and that is good enough for now.

First things first, I had to create the basic page to view the information. I achived this by hacking together a copy of category.cfm and some code from the main index page.

<cfsetting enablecfoutputonly="true">
<cfprocessingdirective pageencoding="utf-8">
    Name : viewByCategory.cfm
    Author : Jon Hartmann
    Created : Dec 3, 2008
    Last Updated :
    History :

    <cfif neq 0>
        <cfset cat =>
        <cfparam name="" default="#cat.categoryname#">
        <cfparam name="form.alias" default="#cat.categoryalias#">
        <cfparam name="" default="">
        <cfparam name="form.alias" default="">
        <cflocation url="categories.cfm" addToken="false">

<cfset params["bycat"] = />
<cfset entries = />

<cfmodule template="../tags/adminlayout.cfm" title="View Entries By Category">

    Your blog currently has #entries.recordcount# "#cat.categoryName#" <cfif entries.recordCount eq 1>entry<cfelse>entries</cfif>.

    <cfmodule template="../tags/datatable.cfm" data="#entries#" editlink="entry.cfm" label="Entries"
             linkcol="title" defaultsort="posted" defaultdir="desc" querystring="">

        <cfmodule template="../tags/datacol.cfm" colname="title" label="Title" />
        <cfmodule template="../tags/datacol.cfm" colname="released" label="Released" format="yesno"/>
        <cfmodule template="../tags/datacol.cfm" colname="posted" label="Posted" format="datetime" />
        <cfmodule template="../tags/datacol.cfm" colname="views" label="Views" format="number" />
        <cfmodule template="../tags/datacol.cfm" label="View" data="<a href=""#application.rooturl#/index.cfm?mode=entry&entry=$id$"">View</a>" sort="false"/>

<cfsetting enablecfoutputonly="false">

Now I need code to reach that page. Primarily I wanted to be able to get to it from the categories page, so I added in another column there:

<cfmodule template="../tags/datatable.cfm" data="#categories#" editlink="category.cfm" label="Categories"
         linkcol="categoryname" linkval="categoryid">

    <cfmodule template="../tags/datacol.cfm" colname="categoryname" label="Category" />
    <cfmodule template="../tags/datacol.cfm" colname="entrycount" label="Entries" />
    <cfmodule template="../tags/datacol.cfm" label="View Entries" data="<a href=""#application.rooturl#/admin/viewByCategory.cfm?id=$categoryid$"">View</a>" sort="false"/>

In order to get this to work though, I had to hack up the datatable.cfm custom tag. Starting on line 180 you'll find a list of substitutions to work with the $something$ notation seen above. There is a note from Ray Camden saying it needs to be dynamic, but since it isn't yet, I added the following code the list of conditionals:

<cfif findNoCase("$categoryid$", value)>
    <cfset value = replace(value, "$categoryid$", categoryid, "all")>

That got me up and running. Lastly, I wanted to add a link from the category.cfm page as well, since I thought that that might be useful. I added a link right in the middle of the form like so:

<form action="category.cfm?" method="post">
        <td align="right">name:</td>
        <td><input type="text" name="name" value="" class="txtField" maxlength="50"></td>
        <td align="right">alias:</td>
        <td><input type="text" name="alias" value="#form.alias#" class="txtField" maxlength="50"></td>
        <td> </td>
        <td><a href="viewByCategory.cfm?">View Entries in this Category</a></td>
        <td> </td>
        <td><input type="submit" name="save" value="Save"> <input type="submit" name="cancel" value="Cancel"></td>


Related Blog Entries

Comments are not allowed for this entry.
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.