Append class if condition is true in Haml

后端 未结 5 1479
温柔的废话
温柔的废话 2020-11-30 17:50

If post.published?

.post
  / Post stuff

Otherwise

.post.gray
  / Post stuff

I\'ve implemente

5条回答
  •  孤街浪徒
    2020-11-30 18:39

    HAML has a nice built in way to handle this:

    .post{class: [!post.published? && "gray"] }
    

    The way that this works is that the conditional gets evaluated and if true, the string gets included in the classes, if not it won't be included.

提交回复
热议问题