36

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?

ylluminate
  • 12,102
  • 17
  • 78
  • 152
jay
  • 12,066
  • 16
  • 64
  • 103

3 Answers3

63

How about

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

or even

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

or

.sample *{:class => [@variable1.name, @variable2.name]}
Zabba
  • 64,285
  • 47
  • 179
  • 207
  • thanks. all three work. the first and second are the nicest format, but they all function. thanks again! – jay Oct 12 '12 at 03:58
  • 8
    Another option, which I prefer: `div class="sample #{@variable.name}"` – Nick B Oct 18 '14 at 00:15
9

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}"
steel
  • 11,883
  • 7
  • 72
  • 109
0

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
staxim
  • 1,328
  • 14
  • 15