Ruby: Sinatra で Erb テンプレートの利用

erb (Ruby 標準のテンプレートエンジン) を呼び出す

Ruby: main.rb

require 'sinatra'
require 'sinatra/reloader'

get '/'
	@title = "hello sinatra"
	@content = "main content"
	erb :index
end

get '/about'
	@title = "hello sinatra"
	@content = "main content"
	@mail = "hoge@hige.hage"
	# erb テンプレートを呼び出し
	erb :about
end

views/layout.erb を作っておくと、自動で読み込んでくれるので使っておく

Erb: views/layout.erb

<!DOCTYPE html>
<html lang="ja">
    <head>
        <meta charset="utf=8">
        <title><%= @title %></title>
    </head>
    <body>
        <%= yeild %>
    </body>
</html>

Erb: views/index.erb

<p><%= @content %></p>

Erb: views/about.erb

<p><%= @content %></p>
<p><%= @mail %></p>