Haml:如果条件在 Haml 中为真,则追加类 [英] Haml: Append class if condition is true in Haml
本文介绍了Haml:如果条件在 Haml 中为真,则追加类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果post.published?
.post
/ Post stuff
否则
.post.gray
/ Post stuff
我已经用 rails helper 实现了这个,但它看起来很丑.
I've implemented this with rails helper and it seems ugly.
= 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的方法?
Is there a more simple and haml-specific way?
更新.特定于 Haml,但仍然不简单:
UPD. Haml-specific, but still not simple:
%div{:class => "post" + (" gray" unless post.published?).to_s}
/ Post stuff
推荐答案
.post{:class => ("gray" unless post.published?)}
这篇关于Haml:如果条件在 Haml 中为真,则追加类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文