如果条件在Haml中为true,则追加类 [英] Append class if condition is true in Haml

查看:68
本文介绍了如果条件在Haml中为true,则追加类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果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?

UPD. 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中为true,则追加类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆