A member of the Liquid Templates group contributes an 18-line extension to Liquid that lets you specify which attributes, methods, and associations to expose to liquid templates, like so:

class SomeClass     # could be an AR model   liquid_methods :attrA, :methB, :associationC, ...  ...end

Just for ducks, here’s the entirety of his extension code, sans comments:

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  endend

Veddy nize.

Link: [extension for drops - Liquid Templates Google Groups](http://groups.google.com/group/liquid-templates/browse_thread/thread/bf48cfebee9fafd9?hl=en “extension for drops - Liquid Templates Google Groups”).

blog comments powered by Disqus


06 July 2007


hacking ruby/rails