From: Shin-ichiro Hara Date: 1999-02-05T19:36:55+09:00 Subject: [ruby-dev:4748] Re: cookie on cgi-lib.rb 原です。 In message "[ruby-dev:4680] Re: cookie on cgi-lib.rb" on 99/02/04, Daisuke Kanda writes: | | | かんだです。 | ぼくのだとこうかな。 | |html = Element::new("html") |html.insert(head = Element::new("head")) | head.insert(title = Element::new("title")) | title.insert("ruby home") |html.insert(body = Element::new("body")) | body.insert(msg1 = Element::new("P")) | msg1.insert("ruby is ") | msg1.insert(Element::new("BR")) | msg1.insert("beautiful!") | body.insert(form = Element::new("Form")) | form.insert(in1 = Element::new("input")) | in1.set_attribute("type", "button") | in1.set_attribute("value", "button") | form.insert(in2 = Element::new("input")) | in2.set_attribute("type", "text") | in2.set_attribute("value", "text") |print html.to_s これ、とても分かります。というか、私自身ほとんど同じものを作って使っ ていたことがあります。html 文書を最初に生成しておいてから、中身を 作って後で一気に出力したいんですよね。html のデータ構造をruby のデー タ構造と保持したい。でもどうしても繁雑になってしまう、、、。CGI.tag はその点使いやすいですね。 思いつきですが、折衷案でこんな感じの仕様はどうでしょう。 h = Element.new h.block("HTML"){ |html| html.block("HEAD").block("TITLE") << "ruby home" html.block("BODY") { |body| body.block("P") << "ruby is beautiful!\n" body.block("FORM") { |form| form.tag("INPUT", {"type"=>"button", "value"=>"button"}) form.tag("INPUT") { |in2| in2.set_attribute("type", "text") in2.set_attribute("value", "text") } } } } print h ここで、block, tag の返り値やブロックパラメータは皆 Element の オブジェクトです。