Redesigned with Tags

As one of my new year’s resolutions I wanted to redesign my blog and start long-form writing on a regular basis. Just a few days before the end of January, I’m able to check off at least half of that resolution with this iteration of my blog. I wanted to leave this design very sparse and focus on the content, and was quite inspired by the general principles behind the Blaskan theme for Wordpress. I like how it turned out, and I think it will allow me to post both semi-long and short posts.

As part of this redesign I also added tags. Hopefully as I write more the tags will be a good way of organizing and accessing content. If you’re using toto and wanted to implement tags, my solution is pretty simple, but my fork of the project is on github. If you’re feeling ambitious, feel free to use my code as a guide to roll your own solution. I wanted to quickly outline the changes I made as documentation for myself and anybody who is looking to implement tags on their site.

The first change I made was to the 'archive' method. I added the logic to filter the articles by a single tag. Here’s the updated method:

The main change starts on line 9, but don’t forget to add the 'tag' variable to the method signature (line 1). The logic is pretty straightforward. Basically, if there is a tag passed (a url like /tag/testtag), we need to iterate through the articles and check if testtag is contained. If there are any articles with that tag, they’re returned.

The next change I made was to the 'go' function, which is how toto decides which URLs get directed where. You need to put this 'elsif' clause after the 'if' and before 'elsif respond_to?(path)'. Again, simple logic, if the first part of the URL is 'tag' and there are two portions of the URL, we’ll call the 'archive' method we just edited.

The last thing we need to do is add the 'tags()' method to the 'Article' class. You can add this declaration anywhere inside the class definition, but I put it right by the other attributes that are being pulled from the YAML (do a search for 'def title' and you should find it).

And that’s it! Now you can include as many tags as you want at the top of your articles in a comma-delimited list such as 'tags: test, anothertest, yetanotherone'. My solution assumes that tags don’t have spaces, but you could modify the code to support tags with spaces if you wished.

Again, the complete code is on github. If you have any questions or comments about this article, @reply me.

Posted by @mclaughj Tagged with #redesign, #code, #development, and #ruby.