In Custom Keywords for BlogCFC Entries Part 1 I described how I updated the administration area to allow for specifying keywords for individual blog entry pages. Now I'm going to review the necessary front-end changes need to be made to utilize these new keywords.
Update Blog.cfc
First things first, I need to get the keywords that I want to display. The article data is retrieved from the database via the getEntries and getEntry functions in blog.cfc. A search for "getEm" takes me directly to the query I need to update inside the getEntries function. This query is pretty straightforward. So it's easy to see that I just want to add tblblogentries.pageKeywords into the list of columns returned. Because I only want these keywords if I'm showing the full article, I placed my new column within this code block:Likewise, a search for "getIt" takes me to the query I want inside getEntry. Again, I just need to add my new pageKeywords field to the query select list.
With these 2 minor updates, our component has been updated to bring back our new data!
Let's show the keywords!
This is easy. In index.cfm I just need to add the new pageKeywords into the "data" structure to make them available during rendering in the layout.cfm template.This is done by adding this line:
2 <cfset data.title = articles.title[1]>
3 <cfset data.entrymode = true>
4 <cfset data.entryid = articles.id[1]>
5 <cfif not structKeyExists(session.viewedpages, url.entry)>
6 <cfset session.viewedpages[url.entry] = 1>
7 <cfif not findnocase(cgi.REMOTE_ADDR,application.ExcludeIPfromViewCount)>
8 <cfset application.blog.logView(url.entry)>
9 </cfif>
10 </cfif>
11</cfif>
I have the data coming from the database to the index page. Now I just need to use it! This is handled in tags/layout.cfm.
We want this to take affect during execution of the module so at the top of layout.cfm I added this code:
2 <Cfif isdefined('attributes.pageKeywords') and len(attributes.pageKeywords)>
3 <cfset keywords = attributes.pageKeywords>
4 <cfelse>
5 <cfset keywords =application.blog.getProperty("blogKeywords") >
6 </Cfif>
after this line:
I did this to catch all the articles created before I had an option for page keywords. In the database, the field is NULL. I don't want to not have keywords on these pages. I opt instead to fall back on the site-wide keywords. I could easily write a quick query to update all NULL pageKeywords to now have my site-wide keywords. For now, I have chosen to leave the database entries alone and provide this validation procedure.
There is also an elseif for url.mode=entry. I'll be honest, I don't see where this will be called, but I see that if it IS called, it is going to make call to the getEntry function. I've already updated this function to return the keywords but what if it retrieves one of those with NULL keywords? To validate against this I add this familiar looking snippet to use site-wide keywords:
2 <cfset keywords = entry.pageKeywords>
3 <cfelse>
4 <cfset keywords =application.blog.getProperty("blogKeywords") >
5 </cfif>
With that, all the data has been gathered and validated and I just have to display it. To do this I just updated the keywords meta tag from:
to:
As I said in part 1, this is my first attempt to write up any coding work I have done. I decided to do this for 2 reasons. I always believe that you don't really know something until you can teach it. By writing this I was able to review my code-work and try to explain what I did and why I did it. So, selfishly, I hope this process will lead me to be a better developer. But I also decided to do this as a way of contributing to the Coldfusion community. This is a very specific contribution that may help few, but it's a start. As I become more comfortable with projecting my voice to the interwebs, who knows what this site will hold.
