まちゅダイアリー

フレームワーク Sizuku (2)

2005-06-16

簡単に使い方を。

以下がサンプル。

index.cgi

#!/usr/bin/env ruby
require 'sizuku'

# Action Object
class MyHandler < Sizuku::Handler
  def handle_main(req, res)
    MainPage.new
  end

  def handle_sub(req, res)
    AmritaPage.new
  end
 end

# Presentation Object
class MainPage
  include Sizuku::ErbTemplate

  def text
    'hello world in erb'
  end
end

class AmritaPage
  include Sizuku::Amrita2Template

  def text
    'hello world in amrita2'
  end
end

require 'cgi'
cgi = CGI.new
MyHandler.new('sample.conf').handle(cgi)

main.rhtml

<html>
 <body>
  <p><%= text %></p>
 </body>
</html>

sub.rhtml

<html>
 <body>
 <p amrita_id='text'></p>
 </body>
</html>

上の例では、 http://example.com/index.cgi/main/ にアクセスすると handle_main が呼ばれ、 MainPage と main.rhtml を結合した結果が出力になる。

同じように、http://example.com/index.cgi/sub/ だと handle_sub が呼ばれ、 AmritaPage と sub.html を結合した結果が出力になる。

あんまり複雑なことはできないけど、ちょっとしたCGIを作る分には使えると思う。

あとは、設定ファイルの読み込みと、エラー処理の仕組みが必要だな…。