如果post.published?
.post
/ Post stuff
否则。.post.gray
/ Post stuff
我已使用Rails助手实现了这个功能,但它看起来很丑陋。
= content_tag :div, :class => "post" + (" gray" unless post.published?).to_s do
/ Post stuff
第二种变体:
= content_tag :div, :class => "post" + (post.published? ? "" : " gray") do
/ Post stuff
有没有更简单、更 Haml 特定的方法?
更新:虽然还是 Haml 特定的,但依然不够简单:
%div{:class => "post" + (" gray" unless post.published?).to_s}
/ Post stuff
{class: [('class1',除非condition1),('class2'如果condition2)]}
...等等 - Mohammad AbuShady{ class:[ (:class1 如果 cond1 成立), (:class2 如果 cond2 成立) ] }
- Phrogz.post{class: ("gray" unless post.published?)}
。 - Robert Travis Pierce