Ruby-on-rails slim-lang 的 if-else 嵌套代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13967324/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
slim-lang's if-else nested code
提问by nik7
I want to assign a css class to a part of HTML based on condition in slim-lang
我想根据 slim-lang 中的条件将 css 类分配给 HTML 的一部分
I am doing the following
我正在做以下事情
- if current_user
.title
- else
.title_else
Now how can i write the HTML that should be nested in the one of the class above? Do i need to write HTML in the both ifcondition and elsecondition ?
现在我该如何编写应该嵌套在上述类之一中的 HTML?我需要在if条件和else条件中都编写 HTML吗?
The reason is that the HTML that should be nested in one of the above class should be further intended rightwards. But if i intend it further right, it is including under else condition. How shoule i approach this now ?
原因是应该嵌套在上述类之一中的 HTML 应该进一步向右。但如果我打算进一步正确,它包括在其他条件下。我现在该如何处理?
回答by Tiago Franco
Here's a one liner:
这是一个单班轮:
.thing class=(current_user ? 'large-menu' : 'medium-menu')
h4 Same content, different class
回答by Craig Walker
Since you're just toggling between two attributes, Tiago Franco's answeris certainly sufficient for your purposes. However, there is another way that comes in handy.
由于您只是在两个属性之间切换,因此Tiago Franco 的答案肯定足以满足您的目的。但是,还有另一种方法可以派上用场。
You can use captureto render HTML to a local variable, and then embed that content inside whatever control structure you like. So, if your wrapping content was a bit more complex, you'd do this:
您可以使用capture将 HTML 呈现为局部变量,然后将该内容嵌入到您喜欢的任何控制结构中。所以,如果你的包装内容有点复杂,你会这样做:
- shared_content = capture do
div This is displayed regardless of the wrapper
- if current_user
| You're logged in!
.title
= shared_content
- else
| You're not logged in!
.title_else
= shared_content
I'll often use this to wrap content in a link if there's a URL available:
如果有可用的 URL,我会经常使用它来将内容包装在链接中:
- image = capture do
img src=image_url
- if url.blank?
= image
- else
a href=url = image
回答by PhilG
1) Yes, you need to write HTML into both of the created divs since you want it to be different. If you didn't want it to be different there would be no need for an ifstatement!
1)是的,您需要将 HTML 写入两个创建的 div 中,因为您希望它有所不同。如果您不希望它有所不同,则不需要if声明!
2) I didn't really understand your point, but this code worked perfectly fine with me:
2)我不太明白你的意思,但这段代码对我来说非常好:
- if true
.title
h4 True is true!
- else
.title_else
h4 True is not true!
This code showed a divwith a class of .titleand a h4with the text True is true!inside it.
这段代码显示了div一个类.title和一个里面h4的文本True is true!。
EDIT:Okay now I see what you mean! It is NOTpossible to do something like this because it won't render the h4!
编辑:好的,现在我明白你的意思了!这是不是可以做这样的事情,因为它不会使H4!
- if true
.title
- else
.title_else
h4 Same content, different class
I would recommend you to write a helper which allows you to render partials and then just render that partial from inside both conditions. This means you only have to write that one partial and call it twice.
我建议您编写一个帮助程序,它允许您渲染部分,然后从两个条件内部渲染该部分。这意味着您只需要编写一个部分并调用它两次。
- if true
.title
= render 'footer'
- else
.title_else
= render 'footer'

