本体となるhello.rbと、viewsフォルダに分ける。
viewsフォルダの中には
layout.haml
index.haml
hello.haml
それぞれ以下のように書いてやればいい。
hello.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
require 'rubygems' | |
require 'sinatra' | |
require 'haml' | |
helpers do | |
include Rack::Utils; alias_method :h, :escape_html | |
end | |
get '/' do | |
haml :index | |
end | |
post '/hello' do | |
haml :hello | |
end |
views/layout.haml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%html | |
%body | |
%h1 Hello World | |
= yield |
views/index.haml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%p あなたの名前は? | |
%form{:action=>"/hello", :method=>"POST"} | |
%input{:type=>"text", :name=>"name"} | |
%input{:type=>"submit", :value=>"送信"} |
views/hello.haml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%p== こんにちは、#{h params[:name]}さん! | |
%a{:href=> "/"} 戻る |
前回のhello_haml.rbを含む全体をまとめたものとをgithubに上げておいた。
https://github.com/zakuni/hellohaml
0 件のコメント:
コメントを投稿