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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章