I think liquid is a fantastic template system. Congrats on investigating/using it.
By default, none of the model's methods are available to the liquid template. This is a good thing. You then specify which methods shall be available. (A white list.)
I use an extension to Module which was sent on the mailing list. Complete extension is below. It handles the Liquid::Drop creation for you by adding a simple #liquid_methods method to classes and modules.
Then, in your models, just do:
class Blog
# id
# name
has_many :posts
def latest_posts(n)
posts.latest(n) # using a named scope
end
def latest_10_posts;latest_posts(10); end
liquid_methods :id, :name, :posts, :latest_10_posts
end
I'm not sure offhand how/if you can pass params into a drop. Ask on the Liquid mailing list. I think you can.
Added: I now re-read your question and see that you really want to send in that param to the method. You can send in more than one argument/parameter to a Liquid filter. So you could have a filter:
# Define as a Liquid filter
def latest_posts(blog, n)
blog.latest(n)
end
# then call the filter in a template:
{{ blog2 | latest_posts: 10 }}
# Note that the second param is after the filter name.
In this example, also remember that you'll need to declare liquid methods in the Post class too.
Here is the module extension.
# By dd -- http://groups.google.com/group/liquid-templates/browse_thread/thread/bf48cfebee9fafd9
# This extension is usesd in order to expose the object of the implementing class
# to liquid as it were a Drop. It also limits the liquid-callable methods of the instance
# to the allowed method passed with the liquid_methods call
# Example:
#
# class SomeClass
# liquid_methods :an_allowed_method
#
# def an_allowed_method
# 'this comes from an allowed method'
# end
# def unallowed_method
# 'this will never be an output'
# end
# end
#
# if you want to extend the drop to other methods you can define more methods
# in the class <YourClass>::LiquidDropClass
#
# class SomeClass::LiquidDropClass
# def another_allowed_method
# 'and this is another allowed method'
# end
# end
# end
#
# usage:
# @something = SomeClass.new
#
# template:
# {{something.an_allowed_method}}{{something.unallowed_method}}{{something.another_allowed_method}}
#
# output:
# 'this comes from an allowed method and this is another allowed method'
#
# You can also chain associations, by adding the liquid_method calls in the
# association models.
#
class Module
def liquid_methods(*allowed_methods)
drop_class = eval "class #{self.to_s}::LiquidDropClass < Liquid::Drop; self; end"
define_method :to_liquid do
drop_class.new(self)
end
drop_class.class_eval do
allowed_methods.each do |sym|
define_method sym do
@object.send sym
end
end
def initialize(object)
@object = object
end
end
end
end