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

26,210

Solution 1

How about

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

or even

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

or

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

Solution 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}"
Share:
26,210
jay
Author by

jay

I’m software engineer / software consultant with 11 years experience working with teams in Shanghai, New York, and London. Read more about me at: http://joshuaballoch.github.io/about

Updated on July 03, 2020

Comments

  • jay
    jay almost 4 years

    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?

  • jay
    jay over 11 years
    thanks. all three work. the first and second are the nicest format, but they all function. thanks again!
  • vilner
    vilner over 9 years
    Another option, which I prefer: div class="sample #{@variable.name}"