Haml:属性声明中的If语句 [英] Haml: If statement inside attribute declaration

查看:86
本文介绍了Haml:属性声明中的If语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为style属性设置条件.根据此答案,这样的事情应该是可能的:

I'm trying to put a conditional for a style attribute. According to this answer something like this should be possible:

.hello{:style => (true ? 'color: green' : 'color: red;')}

但是对我而言,style属性根本不会输出.哈姆(Haml)的情况发生了变化吗?我宁愿不为这种简单的逻辑创建一个助手.

But for me the style attribute doesn't get outputted at all. Did things change in Haml? I rather not create a helper for such simple logic.

推荐答案

我会选择简单的路线并添加一行:

I would take the easy route out and add a line:

- color = true ? 'color: green' : 'color: red;'
.hello{:style => color}

更简单的方法是添加另一个类,并根据需要从css/javascript中对其进行控制:

The even easier way is to add a further class and control it from the css/javascript as needs be:

- success_class = true ? 'success' : 'failed'
.hello{:class => success_class}

这会给你<div class='hello success'></div>

三元组似乎也可以工作

require 'haml'
# => true
str = ".hello{:class => true ? 'green' : 'red' }"
# => ".hello{:class => true ? 'green' : 'red' }"
Haml::Engine.new(str).render

# => "<div class='green hello'></div>\n"
str = ".hello{:style => true ? 'color:green' : 'color:red' }"
# => ".hello{:style => true ? 'color:green' : 'color:red' }"
Haml::Engine.new(str).render
# => "<div class='hello' style='color:green'></div>\n"

这篇关于Haml:属性声明中的If语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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