Introduction To Haml

34
Hamlプラグインで ビューをすっきり 記述する (株)永和システムマネジメント サービスプロバイディング事業部 浦嶌 啓太

description

 

Transcript of Introduction To Haml

Page 1: Introduction To Haml

Hamlプラグインでビューをすっきり

記述する(株)永和システムマネジメント

サービスプロバイディング事業部

浦嶌 啓太

Page 2: Introduction To Haml

アジェンダ

•HTMLがいかに駄目かという話

•そこでHamlですよという話

•Hamlの始め方

Page 3: Introduction To Haml

仕事でRailsを始めて早一年

Page 4: Introduction To Haml

ビューを書くのが嫌になりました

       ____     /      \   /  _ノ  ヽ、_  \  /  o゚⌒   ⌒゚o  \  今日もまた、刺身の上に  ¦     (__人__)    ¦  タンポポのせる  \     ` ⌒´     /  仕事が始まるお…

Page 5: Introduction To Haml

HTMLは冗長なので

•書くのがめんどう

•うっかりミス多発

Page 6: Introduction To Haml

ありがちなうっかりミス

Page 7: Introduction To Haml

1. DOCTYPE宣言をコピペし忘れる。

<?xml version="1.0"?><html>

Page 8: Introduction To Haml

<p><strong><%=h @notice %></strong>

2. タグを閉じ忘れる。

Page 9: Introduction To Haml

<div class="content"> <%= yield %><div>

3. タグを閉じ損ねる。

Page 10: Introduction To Haml

<p class="info"><%=h @info %></div>

4. 終了タグを変え忘れる。

Page 11: Introduction To Haml

<div class=<%=h @user.role %>>

5. 属性値をクオートし忘れる。

Page 12: Introduction To Haml

<%- if @content_for_nav -%><div class="navigation"> <%= yield :nav %></div><% end %>

6. 余分な空行を消し忘れる。

Page 13: Introduction To Haml

これはひどい間違い探しゲーム

Page 14: Introduction To Haml

ビューを書くのが大変!│├ 1. 我慢する│   [まちがい]│    人間無理は禁物です。│    それよりも別の手段を探してみませんか?│    ちょっとしたプラグインでなんとかなるかも?│            ↑│         ココがポイント!└ 2. Hamlを使う    [せいかい]

Page 15: Introduction To Haml

Hamlってなに?

• HTMLを生成するためのテンプレート言語

• Railsのビューを記述する言語として使える

Page 16: Introduction To Haml

よくあるレイアウトをHamlにしてみる

Page 17: Introduction To Haml

1 <?xml version="1.0" encoding="utf-8"?> 2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 3 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 4 <html> 5 <head> 6 <title><%=h @title %></title> 7 </head> 8 <body> 9 <%- if @content_for_nav -%>10 <div class="navigation">11 <%= yield :nav %>12 </div>13 <%- end -%>14 <div class="content">15 <%= yield %>16 </div>17 </body>18 </html>

Page 18: Introduction To Haml

1 !!! XML 2 !!! 1.0 3 %html 4 %head 5 %title&= @title 6 %body 7 - if @content_for_nav 8 .navigation 9 = yield :nav10 .content11 = yield

Page 19: Introduction To Haml

3分でわかるHaml文法講座

Page 20: Introduction To Haml

%•HTML要素を生成する

•インデントでネストを表現する

%h1 Hello <h1>Hello</h1>

%ul %li Item

<ul> <li>Item</li></ul>

Page 21: Introduction To Haml

. と #•class属性とid属性を生成する

•divの場合は要素名を省略可能

%p.desc … <p class=”desc”>…</p>

.users #user_1 …

<div class=”users”> <div id=”user_1”>…</div></div>

Page 22: Introduction To Haml

- と =•ERBの <% … %> と <%= … %> に相当

•end の位置はインデントで判断する

%h1= @title <h1><%= @title %></h1>

- @usrs.each do |u| %li= u.id

<% @usrs.each do |u| %> <li><%= u.id %></li><% end %>

Page 23: Introduction To Haml

!!!•XML宣言やDOCTYPE宣言を生成する

!!! XML <?xml version=’1.0’ encoding=’utf-8’ ?>

!!! 1.1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "...">

Page 24: Introduction To Haml

1 !!! XML 2 !!! 1.0 3 %html 4 %head 5 %title&= @title 6 %body 7 - if @content_for_nav 8 .navigation 9 = yield :nav10 .content11 = yield

Page 25: Introduction To Haml

続きはWebで!

http://haml.hamptoncatlin.com/

Page 26: Introduction To Haml

Hamlの考え方

•HTMLの生成に特化することで

•less codeとDRYを実現する

•ありがちなミスの発生を防ぎ、きれいなコードを生成する

Page 27: Introduction To Haml

•余計なことを考えず、本来やりたいことに注力できる

•これなら書いてやろうかという気分になる

つまり…

Page 28: Introduction To Haml

Hamlの始め方

Page 29: Introduction To Haml

インストールと使い方

$ gem install haml$ haml --rails path/to/rails_root

•ERBと同じ場所にファイルを配置するapp/views/foo/index.html.haml

Page 30: Introduction To Haml

現実と折り合うには

•いきなり全部Hamlに置き換えるのは難しい…

Page 31: Introduction To Haml

トロイの木馬作戦

•HamlはERBと共存できる

•新しく作るビューをHamlにする

•partialをHamlにする

•徐々にHaml率を高めていく

Page 32: Introduction To Haml

まとめ

✓Hamlを使うとやりたいことに注力できる

✓Hamlを使うとビューを書く気分になる

✓Hamlは簡単に使い始められる

Page 33: Introduction To Haml

.happy.coding .with#haml

Page 34: Introduction To Haml

ご静聴ありがとうございました