views:

542

answers:

3
+1  Q: 

Liquid plugin

I am using liquid plugin in my app. While using rails helper methods like form_for,check_box_tag i am getting an error form_for not defined..

Has anybody know how to use rails helper methods through liquid plugin?

A: 

Take a look at this project for Liquid called "Clots". It supposedly addresses this issue (I haven't personally used it).

http://github.com/ludicast/clots/tree/master

Barry Gallagher
A: 

Has anybody know how to use rails helper methods through liquid plugin?

Liquid has its own helpers, called filters. See http://wiki.github.com/tobi/liquid/liquid-for-designers.

You can either decide to apply the helper before passing the value to liquid or extend liquid registering your own filters (http://wiki.github.com/tobi/liquid/liquid-for-programmers).

If you register liquid as Rails template handler, Liquid tries to use your helpers as filters. http://wiki.github.com/tobi/liquid/getting-liquid-to-work-in-rails However, you need to use the Liquid syntax.

{{ 'This is a long section of text' | truncate: 3 }}

Not

{{ truncate('This is a long section of text', 3) }}
Simone Carletti
+1  A: 

I was just dealing with this not too long ago - if you want a better understanding of what it takes to extend the normal filters (including helper methods you may want) I found this Railscast very helpful: http://railscasts.com/episodes/118-liquid

Basically, you'll need to set up your own filter file and include the helper modules you want to use, then add a method (filter) which uses that helper. It's not hard, just takes a second to setup. In Ryan's example he sets up a module in lib called LiquidFilters, includes the number helper he wanted and set it up to use a currency filter like so:

# lib/liquid_filters.rb
module LiquidFilters
  include ActionView::Helpers::NumberHelper

  def currency(price)
    number_to_currency(price)
  end
end

Then all you have to do is remember when you're parsing the liquid content to add :filters => [LiquidFilters] (takes an array of filter modules you want to use) and it should pick it up automatically. This method also makes sure that if you want to set up any more custom filters or modify the helper filters you have an easy and intuitive place to do that.

PJ