ruby - Ruhoh - 每 x 项插入标签

标签 ruby mustache ruhoh

我是 Ruby 和 Ruhoh 的新手,我正在尝试做类似“Rails each loop insert tag every 6 items?”的事情,但我正在使用 Ruhoh。

基本上,我有一个帖子列表,每 3 个帖子我想创建一个新的行 div。

我查看了所有 Ruhoh 文档,似乎没有一种简单的方法可以做到这一点。我想我需要在 Ruhoh 中为一个集合创建一个插件,但是没有 ruby​​ 经验我真的不明白我在做什么。任何正确方向的帮助或指导都会很棒,

干杯。

最佳答案

我自己对 ruby​​ 还很陌生,但是我认为这个解决方案可以满足您的需求!

plugin 目录中创建一个名为 pages_collection_view_addons.rb 的新文件(如果它尚不存在)。

将此添加到该文件:

module PagesCollectionViewAddons
  def chunks(n = 3)
    # Get all the pages
    pages = all
    chunks = []

    # Split the 'pages' array into chunks of size n
    pages.each_slice(n) { |slice|
        chunks.push({pieces: slice})
    }
    chunks
  end
end

# Inform Ruhoh of this new addon
Ruhoh::Resources::Pages::CollectionView.send(:include, PagesCollectionViewAddons)

在您的模板中添加如下内容:

{{# posts.chunks}}
  <div class="row">
       {{# pieces }}
          <h1>{{ title }}</h1>
       {{/ pieces }} 
  </div>
{{/ posts.chunks }}

这将遍历每个 block ,每个 block 看起来像:

{pieces: [post1, post2, post3]}

希望这对您有所帮助。

关于ruby - Ruhoh - 每 x 项插入标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24041490/

相关文章:

javascript - 如何从json渲染mustache js

javascript - 使用正则表达式在 HTML 中查找 Mustache 变量

javascript - iCanHaz.js - 分离模板?

ruby - 为什么 relative_require 在 Ruby 1.8.6 上不起作用?

ruby-on-rails - 在 Rails 中进行通知

ruby-on-rails - 使用 Rails 在 PaperClip 中文件上传 Base64 编码的字符串

ruby-on-rails - rails : can I use elasticsearch-model and active_model_serializers together?