How to write a simple Liquid filter for Mephisto

When I first started customizing the scribbish theme for the site, I wanted links in the ‘meta’ section to submit the current article to social bookmarking sites like del.icio.us and digg. Hey, everyone else does it, right? I did a little digging and wound up with the following:

<a href="http://del.icio.us/post?url=http://www.benlog.org/{{ article.url }}&title={{ article.title }}">del.icio.us</a>
<a href="http://www.digg.com/submit?phase=2&http://www.benlog.org{{ article.url }}&title={{ article.title}}">digg this</a>

Pretty simple feat, sure, but it’s just not doing it for me. Who wants these hacked up urls polluting their template code? Who knows, I might want to re-use these links elsewhere, so it couldn’t hurt if they were a little simpler, say, done via a Liquid filter. What if we could write this instead?

{ article | link_to_delicious }
{ article | link_to_digg : 'digg this' }

Wow, that’s much easier on the eyes. Okay, so that’s what I want, now how do I pull it off? I thought this would be the perfect opportunity to learn how to write custom Liquid filters for Mephisto, and share it with you guys.

First, generate the plugin skeleton. I’ve decided to name our plugin MephistoArticlePlug.

# ruby script/generate plugin MephistoArticlePlug
    create  vendor/plugins/mephisto_article_plug/lib
    create  vendor/plugins/mephisto_article_plug/tasks
    ...
    create  vendor/plugins/mephisto_article_plug/lib/mephisto_article_plug.rb
    create  vendor/plugins/mephisto_article_plug/tasks/mephisto_article_plug_tasks.rake
    create  vendor/plugins/mephisto_article_plug/test/mephisto_article_plug_test.rb

Toss install.rb and uninstall.rb – we won’t use them. Open up lib/mephisto_article_plug.rb, and let’s write the filter method for a del.icio.us link.

module MephistoArticlePlug
  HOST = 'http://www.benlog.org'

  def link_to_delicious(article, text = nil)
    url =  'http://del.icio.us/post?'
    url += "url=#{HOST}#{article['url']}&title=#{article['title']}" 
    content_tag :a, text || 'del.icio.us', :href => url
  end
end

This method takes two parameters – an Article object and some optional text to override the default link name. It builds the same del.icio.us submission url I posted above, but returns the complete link tag. Pretty simple, and I’m sure it could be better written too.

Now we just need to register this new filter with Liquid. Open up init.rb from your plugin directory and write the following.

require 'mephisto_article_plug'
Liquid::Template.register_filter MephistoArticlePlug

The first line is standard Rails fare – it tells Mephisto to include our new plugin. The second line registers every MephistoArticlePlug method as a global filter with Liquid. If you decide to add more methods to the MephistoArticlePlug module, say link_to_digg, they’ll be registered automatically too.

Restart the server, and your new filters are ready to be called from anywhere within your template code. Pretty easy, huh?

Once again, here’s the filter being used in my template, the result of which can be seen below this article.

<li>
  Share:
  {{ article | link_to_delicious }},
  {{ article | link_to_digg }}
</li>

If you’d like more information on Liquid filters, I recommend checking out the following articles.

Commentary

Great article mate – definitely need more of this on the mephisto web site!

Comment by Trent on December 11, 2006

Great article thanks!

Comment by Justin on December 30, 2006