6

今週末、Liquid テンプレート エンジンをいじってみました。

モデルにlatest_postsメソッドがあり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_postsblog.last_post、 のようなことをどのように行うかわかりませんblog.latest_posts: 10

誰でも正しい方向に向けることができますか?

私が考えた 1 つのアイデアは、Liquid フィルターを作成し、Blog オブジェクトと整数の両方をそれに渡すことでした。何かのようなもの:

{% for post in blog | latest_posts(10) %}
  • しかし、私は暗闇の中で少し刺しているような気がするので、まだ試していません. 経験豊富な Liquid ユーザーからの助けをいただければ幸いです。
4

2 に答える 2

9

ここで自分の質問に答えて、Liquid グループのページに記載されている解決策を見つけました。

基本的に、最新の投稿用のドロップを作成する必要がありました---メソッドLatestPostsDropを使用してそれに変数を渡すハックのようなものです。before_method完全なソリューションは次のとおりです。

class Blog

  has_many :posts

  def latest_posts
    LatestPostsDrop.new(posts)
  end

  def to_liquid
    {
      'all_posts' => posts.all,
      'last_post' => post.last,
      'latest_posts' => latest_posts
    }
  end

end

class LatestPostsDrop < Liquid::Drop

  def initialize(posts)
    @posts = posts
  end

  def before_method(num)
    @posts.latest(num)    # Post.latest is a named scope
  end

end

上記を実行すると、次のようなものを使用して、任意の数の最新の投稿を反復処理できます。

{% for post in blog.latest_posts.10 %}  # the last attribute can be any integer
  <p>{{ post.title }}</p>
{% endfor %}

少しハックなようですが、機能します:)

于 2010-08-29T20:15:52.343 に答える
6

Liquid は素晴らしいテンプレート システムだと思います。調査/使用おめでとうございます。

デフォルトでは、モデルのメソッドはいずれも Liquid テンプレートで使用できません。これは良いことです。次に、使用できるメソッドを指定します。(ホワイトリスト)

メーリング リストで送信された Module の拡張機能を使用します。完全な拡張は以下です。クラスとモジュールに単純な #liquid_methods メソッドを追加することで、Liquid::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

パラメータをドロップに渡す方法/かどうかはわかりません。Liquidメーリングリストで質問してください。できると思います。

追加:あなたの質問を読み直したところ、本当にそのパラメータをメソッドに送信したいことがわかりました。複数の引数/パラメーターを Liquid フィルターに送信できます。したがって、フィルターを使用できます。

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

この例では、Post クラスでもリキッド メソッドを宣言する必要があることに注意してください。

これがモジュール拡張です。

# 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 
于 2010-08-29T19:30:06.647 に答える