HAML / Ruby Loop变量附加Content_For

问题描述 投票:-1回答:2

我有一个YAML产品文件:

# Product - Granola Bar
-   title       : "Granola Bar"
    excerpt     : "Comes in several amazing varieties including nut, and delicious fruit."
    description : "Comes in several amazing varieties including nut, and delicious fruit."
    image       : "granola-bar.jpg"
    cost        : "3.50"
    discount    : "2.50"
    stock       : "100"
    ingredients :
        - value : key

# Product - Granola Bar
-   title       : "Hazelnut Spread"
    excerpt     : "Chocolate"
    description : "Chocolate"
    image       : "hazelnut-spread.jpg"
    cost        : "2.50"
    discount    : "1.50"
    stock       : "100"
    ingredients :
        - value : key

当我遍历这些内容时,内容将附加到前一个内容的变量中:

"Comes in several amazing varieties including nut, and delicious fruit. 3.50"
"Comes in several amazing varieties including nut, and delicious fruit. 3.50 Chocolate 2.50"

但是应该是:

"Comes in several amazing varieties including nut, and delicious fruit. 3.50"

然后

"Chocolate 2.50"

我正在使用:

-# Create new array for the thumbnails
- thumbnails  = []

-# Loop through the array
- data.products.each do | product |

    -# Content
    - content_for :content do

        -# Excerpt
        = product.excerpt

        -# Cost
        = product.cost

    - content = yield_content :content

    - thumbnails << { :"data-src" => "holder.js/300x200/auto",
                      :caption    => content,
                      :title      => product.title }

我不确定要搜索什么,循环中的可变变量?我尝试将content = nil放在循环之前和内部,以便在每次通过时都将其取消设置。

我想保存content_for放入部分。

这是正在发生的事情的图像。显然不应在第二个缩略图中将其加倍:

“在此处输入图像描述”

ruby haml middleman
2个回答
2
投票

content_for用于捕获在其他地方使用的内容。

如果您的应用程序布局中有yield_content :title,并且将其设置在当前渲染的局部中:

# index.haml
- content_for :title, "I'm on the index page"

您的示例完全不应该使用content_for。它应该只将字符串呈现到输出中:

- data.products.each do |product|
  = product.excerpt
  = product.cost

如果要在img_tag助手中使用该输出或类似的东西,则如果逻辑比较复杂,则可以使用助手方法来生成内容或将其放入自己的部分中。

您还可以在视图代码中分配新变量,并在以后使用它们:

- caption = "#{product.excerpt} #{product.cost}"

在您的示例中,我根本不需要这样的东西。也许是因为您实际上从未在任何地方使用“缩略图”。


-2
投票

最后我改成了使用capture_html

    -# Loop through the array
- data.products.each do | product |

    -# Content
    - capture = capture_html do

        -# Excerpt
        %p
            = product.excerpt

        -# Page Name
        - page = product.title.downcase.gsub( /[^a-z0-9\- ]/, ' ').gsub( / /, '-' )

        -# Button
        = partial "bootstrap/css/button",
                   :locals => { :color      => "primary",
                                :content    => "View Product",
                                :extraClass => "pull-right",
                                :href       => "/shop/#{ page }" }

        -# Cost
        %p.text-left.price
            &pound;
            = product.cost

        .clearfix

    - thumbnails << { :"data-src" => "holder.js/300x200/auto",
                      :caption    => capture,
                      :title      => product.title  }
© www.soinside.com 2019 - 2024. All rights reserved.