LoginSignup
0
0

More than 3 years have passed since last update.

scssの入れ子とパーシャル

Posted at

自分用です!

sassのimport

railsでは、cssをsassで書いていきます。

・scssにすることで変数や親子関係の定義など、効率化の機能が使用でき、全体のコード量を削減できます。
・sass記法ではインデント制御のためデザイナーに引き渡しづらく、cssの記法から離れてしまうという面からscss記法を採用しています。

application.scssとtop.scssがあるとして、top.scssをapplication.scssにインポートします。

application.scss
@import 'top';

このように、@importを使えば必要なcssファイルだけをインポートできます。

パーシャル

次に、sassと同じようにviewでも入れ子構造が使えます。
それがパーシャルです。

application.html.erbにheaderとfooterのコードがたくさん入っていていると見えずらいですよね。

そこで、shared/_header.html.erbと、shared/_footer.html.erbを作成し、そこにヘッダーとフッターのコードをかき、application.htmlに入れ子する。

application.html.erb
<body>
  <%= render 'shared/header' %>
  <%= yield %>
  <%= render 'shared/footer' %>
</body>

このようにすれば、見やすくなる。

0
0
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
0
0