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

天涯浪子 提交于 2019-11-28 18:42:25

问题


In rails slim (http://slim-lang.com/) the syntax for defining a new div with a class name "sample" is the following:

 .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:

 <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?


回答1:


How about

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

or even

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

or

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



回答2:


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}"



回答3:


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


来源:https://stackoverflow.com/questions/12851843/ruby-slim-how-do-you-define-an-elements-class-with-a-rails-helper-or-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!