Html Ruby Slim - 如何使用 rails 助手或变量定义元素的类?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12851843/
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
Ruby Slim - How do you define an element's class with a rails helper or variable?
提问by jay
In rails slim (http://slim-lang.com/) the syntax for defining a new div with a class name "sample" is the following:
在 rails slim (http://slim-lang.com/) 中,定义一个类名为“sample”的新 div 的语法如下:
.sample
= "Content goes here"
this will create:
这将创建:
<div class="sample">
Content goes here
</div>
I want to define a div's class according to a rail's helper, a variable, or other things.. such as, in rails:
我想根据 rails 的 helper、变量或其他东西来定义一个 div 的类......例如,在 rails 中:
<div class="sample #{@variable.name}">
Content goes here
</div>
I have no idea how to do this in slim:
我不知道如何在苗条中做到这一点:
.sample #what else goes here?
Content goes here
Anyone know how?
有谁知道怎么做?
回答by Zabba
How about
怎么样
div[class="sample #{@variable.name}"]
or even
甚至
div class=["sample", @variable.name]
or
或者
.sample *{:class => [@variable1.name, @variable2.name]}
回答by steel
You can use parentheses, curly braces or just a space
您可以使用括号、花括号或仅使用一个空格
.first-class(class="second-class-#{ruby_call}")
.first-class *{class: "second-class-#{ruby_call}"}
.first-class class="second-class-#{ruby_call}"
回答by staxim
For slim templates, I've been using the parenthesis notation. You can add additional content on the same line with the equals sign ("="), but be sure to include a space:
对于瘦模板,我一直在使用括号表示法。您可以使用等号 ("=") 在同一行添加其他内容,但一定要包含一个空格:
h1 class=("sample #{@variable.name}") = @variable.to_s