EastsideDev
EastsideDev

Reputation: 6639

Dynamic class name in SLIM used in Rails

I am using SLIM with Rails, and would like to optimize one of my views.

I have three alert SCSS classes:

alert-notice
alert-success
alert-danger

A value: alert-type is passed to the view as a string, with the following possibilities:

notice
success
danger

I can write a convoluted piece of code, something like this:

- if alert-type == 'notice'
    .alert.alert-notice[role="alert"]
      button.close[type="button" data-dismiss="alert" aria-hidden="true"]
  elsif alert-type == 'success'
    .alert.alert-success[role="alert"]
      button.close[type="button" data-dismiss="alert" aria-hidden="true"]
  else
     .alert.alert-danger[role="alert"]
       button.close[type="button" data-dismiss="alert" aria-hidden="true"]

But this seems to be very inefficient. Any ideas?

EDIT:

Something like this, would work:

|  <div class="alert alert-
= name.to_s == 'notice' ? 'success' : 'danger'
| " role="alert"> 
button.close[type="button" data-dismiss="alert" aria-hidden="true"]
  | ×

But this code is kind of ugly, and was wondering if I can use the simplified SLIM syntax: .alert.alert......

Upvotes: 0

Views: 1899

Answers (1)

jvillian
jvillian

Reputation: 20263

I'll apologize in advance that I don't use slim. But, based on a quick search, it seems like it would be something like

div role = "alert" class = "alert alert-#{alert-type}"
  button.close[type="button" data-dismiss="alert" aria-hidden="true"]

It also looks like you should be able to do:

.alert[role = "alert" class = "alert-#{alert-type}"]
  button.close[type="button" data-dismiss="alert" aria-hidden="true"]

Upvotes: 3

Related Questions