slim-lang的if-else嵌套代码
我想根据slim-lang中的条件为一部分HTML分配一个css类
我正在做以下事情
- if current_user .title - else .title_else
现在我如何编写应该嵌套在上面一个类中的HTML? 我是否需要在if
条件和else
条件下编写HTML?
原因是应该嵌套在上述类之一中的HTML应该是向右的。 但是,如果我打算进一步正确,它包括在其他情况下。 我现在怎么样呢?
这是一个class轮:
.thing class=(current_user ? 'large-menu' : 'medium-menu') h4 Same content, different class
由于你只是在两个属性之间切换, Tiago Franco的回答肯定足以满足你的目的。 但是,还有另一种方法可以派上用场。
您可以使用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
如果有可用的URL,我会经常使用它来包含链接中的内容:
- image = capture do img src=image_url - if url.blank? = image - else a href=url = image
1)是的,您需要将HTML写入两个创建的div中,因为您希望它们不同。 如果你不希望它不同,就不需要if
语句了!
2)我真的不明白你的意思,但这段代码对我来说非常好:
- if true .title h4 True is true! - else .title_else h4 True is not true!
这段代码显示了一个带有.title
类的.title
和一个带有文本True is true!
的h4
True is true!
在里面。
编辑:好的,现在我明白你的意思了! 不可能做这样的事情,因为它不会渲染h4!
- if true .title - else .title_else h4 Same content, different class
我建议你写一个帮助器,它允许你渲染局部,然后从两个条件内部渲染部分。 这意味着你只需要写一个部分并调用它两次。
- if true .title = render 'footer' - else .title_else = render 'footer'