汉姆两个空格的问题 [英] haml two spaces issue

查看:60
本文介绍了汉姆两个空格的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用haml时,我遇到以下问题.

When using haml I have following problem.

首先,我想检查一个变量,然后渲染其他变量,但仍应嵌套.

First I want to check one variable and after that render something else, but it still should be nested.

让我解释一下

代码:

.a
  .b

gives:   <div class=a><div class=b></div></div>

如果我使用haml,则无法将.b嵌套在.a中:

When I use haml if else, I can't nest .b inside .a:

- if id == 3
  .a{style => 'xxx'}
- else
  .a{style => 'yyy'}

    .b <-- 2 spaces, otherwise it fails. but 2 spaces are causing the following issue:

问题是,因为haml没有结束,所以在两种情况下(如果id == 3,否则)我都不知道如何将.b放在.a div中.

The problem is, because there is no end in haml, I don't know how to put .b within .a div, in both situations (if id ==3, or else).

作为一个真实的例子:

- if home != nil
  .home
    home.id
- else
  .home
    empty
- end   <--- can't use
  - if room != nil <-- without end, this will be executed only if home == nil but I wan't this to be executed always
    room.id
  - else
    empty

因为haml不支持-end,所以我不能使用它.如果我不使用它,那么当我开始查看房间"的值时,haml会自动关闭div,因为它们在同一行:)

because haml doens't support -end, I can't use it. If I don't use it, haml automatically closes div if I start to cehck 'room's values because these are in the same line :)

将变量与nil进行比较的示例,或者仅在变量为nil的情况下才进行操作只是一个示例.我正在寻找一种可以通过缩进来解决此类问题的解决方案,以便将其应用于整个语句.

Example with comparing variable to nil, or doing something only if the variable is nil is just an example. I'm looking for a solution that solves such problem with indenting after else so it applies to the whole statement.

存在类似的问题: HAML:使用以下命令缩进if/else语句常见内容

因此,您希望自己这样做:

So, in your case you would do like:

.a{ :style => id == 3 ? 'xxx' : 'yyy' }
  .b

编辑: 如果是if... elsif... elsif...,您可以编写自己的帮助程序:

edited: In case of if... elsif... elsif... you can write your own helper:

.a{ :style => select_style(variable) }
  .b

#helper method
def select_style(val)
  case val
  when "3"
    "xxx"
  when "4"
    "yyy"
  else
  "zzz"
  end
end

您当然可以全部用haml编写,但这很丑陋:

Of course you can write it all in haml, but it will be ugly:

- if id == "3" then val="xxx"
- elsif id == "4" then val="yyy"
- else val="zzz"
.a { :style => val }
 .b

HAML有其优点,但这是缺点之一.

HAML has its advantages, but it is an example of one of the disadvantages.

已编辑

或者您可以生气并喜欢:

Or you can going mad and do like:

.a{:style => if var == "3" then "xxx" elsif var == "4" then "yyy" else "zzz" end}

推荐答案

有类似的问题:因此,您希望自己这样做:

So, in your case you would do like:

.a{ :style => id == 3 ? 'xxx' : 'yyy' }
  .b

编辑: 如果是if... elsif... elsif...,您可以编写自己的帮助程序:

edited: In case of if... elsif... elsif... you can write your own helper:

.a{ :style => select_style(variable) }
  .b

#helper method
def select_style(val)
  case val
  when "3"
    "xxx"
  when "4"
    "yyy"
  else
  "zzz"
  end
end

您当然可以全部用haml编写,但这会很丑:

Of course you can write it all in haml, but it will be ugly:

- if id == "3" then val="xxx"
- elsif id == "4" then val="yyy"
- else val="zzz"
.a { :style => val }
 .b

HAML有其优点,但这是缺点之一.

HAML has its advantages, but it is an example of one of the disadvantages.

已编辑

或者您可以生气并喜欢:

Or you can going mad and do like:

.a{:style => if var == "3" then "xxx" elsif var == "4" then "yyy" else "zzz" end}

这篇关于汉姆两个空格的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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