LoginSignup
3
1

More than 3 years have passed since last update.

Slimとは

Posted at

Railsで使えるテンプレートエンジン:blush:
Slimの他にHamlなどがあるが、Slimの方が記述方法がシンプル。
HTMLをタグではなく階層構造(インデント)で表す。閉じタグは省略可能。

テンプレートエンジンとは

MVCフレームワークにおいて、Controllerで定義した変数をviewで使用できるようにする仕組み。
Railsでは標準でERBが採用されている。

文法

「/」 コメントアウト

test.slim
/これはコメントです
tr
 th
 th

出力

test.html
<tr>
  <th></th>
  <th></th>
</tr>

「-」 Rubyのコードを記述

test.slim
tbody
      - @tasks.each do |task|
        tr
          td
          td

「=」 Rubyのコードを出力する

test.slim
tr
  td= link_to task.name, task
  td= task.created_at

「# / .」 id属性・クラス属性

#tasks
  .task.red
  /idの中に複数クラス

出力

test.html
<div id="tasks">
  <div class="task red">

  </div>
</div>
3
1
0

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
3
1