Blogger Labels in CF Part 2
A few days ago I posted a simple solution for listing Blogger Labels in a classic template using ColdFusion. I wasn't happy with just listing the Labels though, I wanted to know how many entries were in each Label. The code that follows is my solution:
<cfscript>
// Returns number of blog entries in a Blogger Label file
function CountLabels(param) {
ct = 0;
ptr = FindNoCase('<div class="post">', param) + 1;
while (ptr gt 1) {
ct = ct + 1;
ptr = FindNoCase('<div class="post">', param, ptr) + 1;
}
return ct;
}
</cfscript>
<!--- This is the first time running, so we need to create the structures in the application scope --->
<cfif Not(StructKeyExists(application, "structLabels"))>
<cfset application.structLabels = StructNew()>
<cfset application.structEntries = StructNew()>
</cfif>
<!--- Get a directory listing of the Labels folder --->
<cfset pathLabels = "#getDirectoryFromPath(getBaseTemplatePath())#labels\">
<cfdirectory name="dirLabels" action="list" directory="#pathLabels#" sort="name asc">
<cfloop query="dirLabels">
<!--- This is the first time running, so we need to count the number of entries for this Label --->
<cfif Not(StructKeyExists(application.structLabels, dirLabels.name))>
<cffile action="read" file="#pathLabels#\#dirLabels.name#" variable="fileLabel">
<cfset ctLabel = CountLabels(fileLabel)>
<cfset StructInsert(application.structLabels, dirLabels.name, dirLabels.dateLastModified)>
<cfset StructInsert(application.structEntries, dirLabels.name, ctLabel)>
<!--- The Labels file was updated, so we need to recount the number of entries for this Label --->
<cfelseif StructFind(application.structLabels, dirLabels.name) neq dirLabels.dateLastModified>
<cffile action="read" file="#pathLabels#\#dirLabels.name#" variable="fileLabel">
<cfset ctLabel = CountLabels(fileLabel)>
<cfset StructUpdate(application.structLabels, dirLabels.name, dirLabels.dateLastModified)>
<cfset StructUpdate(application.structEntries, dirLabels.name, ctLabel)>
</cfif>
</cfloop>
<ul>
<cfloop collection="#application.structEntries#" item="key">
<!--- If there are entries in a Label file, then list it as a link with the total number of entries --->
<cfif StructFind(application.structEntries,key)>
<li><cfoutput><a href="/blog/labels/#key#">#ListFirst(key,".")#</a> (#StructFind(application.structEntries,key)#)</cfoutput></li>
</cfif>
</cfloop>
</ul>
Labels: Blogger, ColdFusion

