まずインストール
gem install slim
SinatraにSlimが搭載されるのは次のリリースかららしいので、以下が必要。
require 'slim'
module Sinatra
module Templates
def slim(template, options={}, locals={})
render :slim, template, options, locals
end
end
end
で、中身。
helloslim.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' | |
require 'slim' | |
module Sinatra | |
module Templates | |
def slim(template, options={}, locals={}) | |
render :slim, template, options, locals | |
end | |
end | |
end | |
helpers do | |
include Rack::Utils; alias_method :h, :escape_html | |
end | |
get '/' do | |
slim :index | |
end | |
post '/hello' do | |
slim :hello | |
end |
views/layout.slim
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.slim
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.slim
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='/' 戻る |
sassも使えるらしい。
0 件のコメント:
コメントを投稿