rails haml 如果添加 class

rails haml if add class

如何在不重复渲染行的情况下在 haml 中编写此代码?

- if i % 2 == 0
  %section.wrapper-md.list
    = render partial: 'property'
- else
  %section.wrapper-md.list.background-gray
    = render partial: 'property'

谢谢!!

尝试这样的事情

%section.wrapper-md.list{class: ('background-gray' if i.even?)}
  = render partial: 'property'

您也可以尝试使用 cycle 助手,不需要计数器

%section.wrapper-md.list{class: cycle('', 'background-gray')}
  = render partial: 'property'