LoginSignup
8
7

More than 5 years have passed since last update.

Slim で動的に HTML の class を変えたいとき

Last updated at Posted at 2016-01-27

たとえば、こんなことをやりたいとき

table
  thead
    tr
      th name
      th age
  tbody
    - @users.each do |u|
      - if u.age >= 20
        tr.info
          td = u.name
          td = u.age
      - else
        tr.danger
          td = u.name
          td = u.age

これは、ダサいですね。

こう書くと、ちょっとクールですね。

table
  thead
    tr
      th name
      th age
  tbody    
    - @users.each do |u|
      - klass = (u.age) >= 20) ? 'info' : 'danger'
      tr class="#{klass}"
        td = u.name
        td = u.age

もっとよい書き方ありそう。
class の条件が増えていったら大変そうだ。 

8
7
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
7