Haml
今回紹介するもう一つのライブラリHamlは,
実際の例を見てみましょう。以下のコードはHamlでHTMLのレイアウトを表現したものです。
リスト3 HamlによるHTML表現の例
!!! XML
!!! Strict
%html
%head
%title SinatBBS
%meta{:"http-equiv"=>"Content-Type", :content=>"text/html", :charset=>"utf-8"}
%link{:rel=>"stylesheet", :type=>"text/css", :href=>"/style.css"}
%body
%h1 SinatBBS
!= yield
<html>や<body>といったタグの階層構造がインデントで表されます。
もう一つ,
リスト5 HamlによるHTML表現の例
- @comments.each do |comment|
.comment
%h2= h comment.title
.info
%span.name== by #{h comment.name}
%span.date== (#{h comment.date})
.message
= comment.formatted_message
タグ名は
タグ名のあとに
eachメソッドで繰り返しを行う場合,
Sass
Hamlには,
また,
今回のアプリケーションではview/
アプリケーション本体
今回はSequelがモデル部分を,
リスト6 Sinatraによるコントローラ部分
get '/' do
@comments = Comments.order_by(:posted_date.desc)
haml :index
end
put '/comment' do
Comments.create({
:name => request[:name],
:title => request[:title],
:message => request[:message],
:posted_date => Time.now,
})
redirect '/'
end
PUTメソッドはサーバによってはサポートされないため,
Sinatra, その先に
今回は
確かに,
その他,
まとめ
今回はSinatraとSequel,
次回は,