Haml - 明确结束缓存

时间:2015-03-19 08:15:33

标签: ruby-on-rails haml

我有带缓存的_product.html.haml文件:

- cache product do
  %tr{product_id: "#{product.id}"}
    %td.col-md-1.vert-align= image_tag product.image.url(:thumb), class: "img-thumbnail" if product.image?
    %td.col-md-4.vert-align= link_to product.title, product
    %td.col-md-1.vert-align= number_to_currency product.price
    %td.col-md-1.vert-align= product.available ? content_tag(:span, 'Available', class: "available") : content_tag(:span, 'Booked', class: "booked")

    - if user_signed_in? && current_user.is_admin
      %td.col-md-2.vert-align
        = link_to "Edit", edit_product_path(product), class: "btn btn-default btn-xs"
        = link_to "Delete", '#', title: "Delete product", class: "btn btn-danger btn-xs delete-product"

我不需要缓存if底部的块。缓存执行/结束块如何显式结束?

1 个答案:

答案 0 :(得分:3)

如果您希望在其中包含非缓存<tr>,则缓存整个<td>是没有意义的。

您可以缓存4 <td>

%tr{product_id: "#{product.id}"}
  - cache product do
    %td.col-md-1.vert-align= image_tag product.image.url(:thumb), class: "img-thumbnail" if product.image?
    %td.col-md-4.vert-align= link_to product.title, product
    %td.col-md-1.vert-align= number_to_currency product.price
    %td.col-md-1.vert-align= product.available ? content_tag(:span, 'Available', class: "available") : content_tag(:span, 'Booked', class: "booked")

  - if user_signed_in? && current_user.is_admin
    %td.col-md-2.vert-align
      = link_to "Edit", edit_product_path(product), class: "btn btn-default btn-xs"
      = link_to "Delete", '#', title: "Delete product", class: "btn btn-danger btn-xs delete-product"