Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions hello.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<html>
<head><meta charset='utf-8'></head>
<body>
<h1>hello.erb</h1>
<p>これはerbファイルです</p>
<p>webrick.rbにHTMLを書くのは可読性, 保守性が著しく低くなります。</p>
<p>その問題を解決するためにHTMLを書く専用のファイルを用意します。</p>

<h2>現在時刻: <%= @now %></h2>
</body>
</html>
10 changes: 10 additions & 0 deletions webrick.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# webrick.rb
require 'webrick'
require "erb"

server = WEBrick::HTTPServer.new({
:DocumentRoot => './',
Expand Down Expand Up @@ -45,4 +46,13 @@
res.body = body
end

WEBrick::HTTPServlet::FileHandler.add_handler("erb", WEBrick::HTTPServlet::ERBHandler)
server.config[:MimeTypes]["erb"] = "text/html"

server.mount_proc("/hello") do |req, res|
template = ERB.new( File.read('hello.erb') )
@now = Time.new
res.body << template.result( binding )
end

server.start