twitterlink facebooklink feedlink

April 10, 2010: This afternoon I recieved a visit from a plushy black cat. I've never seen her before and since she has a collar she may be moved in with her owner in the last days.

Read more about project 365 ...

Howto write your own plugin for mephisto

Posted by semantosoph on Aug 01, 2009 | 0 comments

On my crusade on gaining more insight into mephisto, I felt it would be helpful to see how this plugin thingy works. I found, that writing your own mephisto plugin is not as hard as you would think. In fact, it is real easy. Start with generating the plugin skeleton.

# ruby script/generate plugin NameOfYourPlugin

Now, define your method inside lib/name_of_your_plugin.rb.

module NameOfYourPlugin

  def link_to_something_strange(object_to_yield)
    url =  'http://example.com/api?'
    url += "id=#{object_to_yield['some_attribute']}" 
    content_tag :a, 'foobar', :href => url
  end
end

After that, open init.rb and paste in the following:

require 'name_of_your_plugin'
Liquid::Template.register_filter NameOfYourPlugin

The first line is the standard registration you need for every rails plugin. The second line registers whatever method you defined as a global liquid filter. If you like to add another filter, just put the method inside lib/name_of_your_plugin.rb and it will be automagically registered.

The last step is to include your fresh filter into the liquid template.

{{ object_to_yield | link_to_something_strange }}

That’s all folks. Really. Be sure to check the first fruit of this new gained insights.

Post your comment

Filed under , , , ,