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'

最新更新