Haml:如果條件在 Haml 中爲真,則追加類 - Haml: Append class if condition is true in Haml

問題:

If post.published?如果post.published?

.post
  / Post stuff

Otherwise除此以外

.post.gray
  / Post stuff

I've implemented this with rails helper and it seems ugly.我已經用 rails helper 實現了這個,它看起來很難看。

= content_tag :div, :class => "post" + (" gray" unless post.published?).to_s do
  / Post stuff

Second variant:第二個變體:

= content_tag :div, :class => "post" + (post.published? ? "" : " gray") do
  / Post stuff

Is there a more simple and haml-specific way?有沒有更簡單和特定於haml的方法?

UPD.更新。 Haml-specific, but still not simple:特定於 Haml,但仍然不簡單:

%div{:class => "post" + (" gray" unless post.published?).to_s}
  / Post stuff

解決方案:

參考一: https://en.stackoom.com/question/EUQa
參考二: https://stackoom.com/question/EUQa
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章