ページ

2010年12月4日土曜日

SinatraでSlimを使う

前々回前回ときて、今度はSlimをSinatraで使う。

まずインストール
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
# -*- 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
view raw helloslim.rb hosted with ❤ by GitHub

views/layout.slim
html
body
h1 Hello World
== yield
view raw layout.slim hosted with ❤ by GitHub

views/index.slim
p あなたの名前は?
form action="/hello" method="POST"
input type="text" name="name"
input type="submit" value="送信"
view raw index.slim hosted with ❤ by GitHub

views/hello.slim
p こんにちは、#{h params[:name]}さん!
a href='/' 戻る
view raw hello.slim hosted with ❤ by GitHub

sassも使えるらしい。

0 件のコメント:

コメントを投稿