繁体   English   中英

Ruby Slim-如何使用rails助手或变量定义元素的类?

[英]Ruby Slim - How do you define an element's class with a rails helper or variable?

在rails slim(http://slim-lang.com/)中,用于定义类名称为“ sample”的新div的语法如下:

 .sample
      = "Content goes here"

这将创建:

 <div class="sample">
      Content goes here
 </div>

我想根据Rails的帮助器,变量或其他东西(例如,在rails中)定义div的类:

 <div class="sample #{@variable.name}">
   Content goes here
 </div>

我不知道如何苗条地做到这一点:

 .sample #what else goes here?
   Content goes here

有人知道吗?

怎么样

div[class="sample #{@variable.name}"]

甚至

div class=["sample", @variable.name]

要么

.sample *{:class => [@variable1.name, @variable2.name]}

您可以使用括号,大括号或仅使用空格

.first-class(class="second-class-#{ruby_call}")

.first-class *{class: "second-class-#{ruby_call}"}

.first-class class="second-class-#{ruby_call}"

对于苗条的模板,我一直使用括号符号。 您可以在同一行上添加带有等号(“ =”)的其他内容,但请确保包含空格:

h1 class=("sample #{@variable.name}") = @variable.to_s

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM