34
28

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[Rails4] Slimで、CSSのセレクタに動的な変数名を入れるときの書き方

Last updated at Posted at 2015-05-06

Rails4でViewコーディングをしていると、@items.each do |item|などで、<li>タグなどに付与するidやclassのセレクタを、動的にしたい(例えば article-1, article-2 など)ときがあります。

RailsのSlimでCSSのクラス指定を動的にする書き方

li[class="item #{item.name}"]

使用例

section[class="content content-#{content.order}"]

【追記】コメントいただきました。こちらのほうがスマートに書けそうです。

li.item class=item.name
34
28
1

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
34
28

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?