Creating a blog in Catalyst
It seems like everyone is doing it – I know of at least two other codebases for a blog controller – which makes me wonder: is there a repository of drop-in components for the Catalyst framework, or are we all doomed to re-invent the wheel? I did ask on #catalyst about it, but having not thought it all through properly I asked if there was a CMS system built on Catalyst (which is something completely different)
In any case, I’m in the middle of a code rewrite on one of my own projects, and so I wanted a blog on there. The old version already had a WordPress ‘blog’ (more like a series of articles), but I wanted to have a separate blog to the articles. After creating a simple DB schema for it, and creating the appropriate Model for it, the fun really began – writing the controller.
Here, I must say, I was extremely impressed – getting the index page (with the list of blog titles on) only took about a dozen lines of code. I actually spent more time getting the HTML/CSS right! I can live with that speed of development.
After the index page came the individual entry page. I overloaded the controller action to take optional parameters (to identify the particular entry) with a fallback to the index if the parameters didn’t actually match a record. Another dozen or so lines of code, once again, more time spent on the frontend stuff.
On a roll, I added a sitemap action to the controller – 3 lines of Perl code and a 12 line Template::Toolkit template. I spent more time reading the sitemap spec than I did coding.
Having more dev time left than I expected, I decided to throw in a copy of my Twitter feed as well. For speed, I decided to cache the tweets in the database, and only search for new tweets on page refreshes. So, another database table, rebuild the models, and back into writing controller code.
Given the choice between Net::Twitter and Net::Twitter::Lite, I went for Net::Twitter::Lite – I didn’t think I needed the extra bells and whistles the ‘full’ version gave, and I didn’t particularly want to drag down half of CPAN as deps! Net::Twitter::Lite installed straight away (I was lazy and put it straight into the Catalyst project lib directory). The code for this was slightly more complicated, due to the date format returned by the Twitter API, and still feeling lazy that went into the controller too (at some point I’ll create a proper Model for it). All in all, though, it was another dozen or so lines of code, making the grand for a simple (read-only – no actions for creating/editing entries yet) blog system with Twitter reporting less than 50 lines of code. (and that’s even with whitespace and comment lines!)
For the articles, I just copied the blog code, removed the Twitter stuff and created a new database table/Model for the entries – there was no point re-inventing the wheel, and brought me full-circle back to my original question – is there a way to create common feature blocks that can just be dropped into a Catalyst project? It’s not as easy as it sounds – Controllers would need the appropriate Models and Views in place to work, which makes packaging tricky (not to overwrite existing parts). Meh, I’m too tired to think about it now, but having a library of building blocks would make building sites using Catalyst even quicker…