English 中文(简体)
将变数通过模型计算方法
原标题:Passing variables to model instance methods in Liquid templates

我本周末与液体排气发动机一道玩.,我很想知道能否做到以下几点。

页: 1 在<代码>Blog模式中采用的方法,我可以通过分类获得最新的N员额。 是否可能在液体模板中使用这种方法?

例如:

class Blog

  has_many :posts

  def latest_posts(n)
    posts.latest(n) # using a named scope
  end

  def to_liquid(*args)
    {
       all_posts  => posts.all,  # allows me to use {% for posts in blog.all_posts %}
       last_post  => post.last,  # allows me to use {% assign recent = blog.last_post %}
       latest_posts  => posts.latest_posts(args[0])  # how do I pass variables to this?
    }
  end

end

在以上简化例子中,在我的液体模板中,我可以使用<代码>blog.all_posts和blog.last_post,但没有任何想法,如blog.latest_posts: 10

谁能把我指向正确的方向?

据认为,一种想法是建立一个液体过滤器,并将Blog物体和ger化物通过。 类似:

{% for post in blog | latest_posts(10) %}
  • but haven t tried that yet as feel like I m stabbing around in the dark a bit. Would appreciate some help from more experienced Liquid users.
最佳回答
问题回答

我认为液体是一种弹性模板系统。 命令调查/使用。

违约后,液体模板无法得到任何模型方法。 这是一件好事。 然后,请具体说明可采用哪些方法。 (白人名单)

我使用邮寄名单上的模块。 全面延长期限如下。 它处理液体:通过在班级和单元中添加简单的“方法”方法,为你创建Drop。

那么,在您的榜样中,我们就是这样:

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

我不敢肯定你如何/如果能够把 para子带入 drop子。 资产负债表上的Ak。 我认为你能够这样做。

<><><> 我现在重新谈了你的问题,并看到你希望把这个段落寄给这个方法。 你可以在不止一个论点/参数上向液体过滤器发送。 因此,你可以有一个过滤器:

# 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.

在这方面,还记得,你也需要在员额类别中宣布清算方法。

此处是单元的延伸。

# 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 




相关问题
Ruby parser in Java

The project I m doing is written in Java and parsers source code files. (Java src up to now). Now I d like to enable parsing Ruby code as well. Therefore I am looking for a parser in Java that parses ...

rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

multiple ruby extension modules under one directory

Can sources for discrete ruby extension modules live in the same directory, controlled by the same extconf.rb script? Background: I ve a project with two extension modules, foo.so and bar.so which ...

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

热门标签