From: "John W. Long" Date: 2003-11-23T01:50:29+09:00 Subject: HTML Generation (Next Generation CGI) Hi, This idea for the next generation of CGI has me thinking (see http://rubygarden.org/ruby?NextGenerationCGI). It seems that CGI would be best if it were broken in two. One side handled generating html, and the other handled all the other stuff (params, headers, etc...). Last night I started prototyping some of my ideas and came up with the following: class HtmlGenerator def render(klass, &blk) hg = klass.new hg.render(&blk) end end class Html4 attr_accessor :encoding def render(&blk) "#{self.instance_eval(&blk)}\n" end def head(&blk) "\n#{self.instance_eval(&blk)}\n" end def title(&blk) "\n#{self.instance_eval(&blk)}" end def meta(hash = {}) "\n" + '' end def body(&blk) "\n#{self.instance_eval(&blk)}\n" end def h1(&blk) "\n

#{self.instance_eval(&blk)}

" end def h2(&blk) "\n

#{self.instance_eval(&blk)}

" end def p(&blk) "\n

#{self.instance_eval(&blk)}

" end def b(&blk) "#{self.instance_eval(&blk)}" end def i(&blk) "#{self.instance_eval(&blk)}" end end hg = HtmlGenerator.new puts hg.render(Html4) { encoding = "US/English" head { title { "My Document" } + meta(:name => "description", :content => "this is a description of this document") } + body { h1 { "Heading 1" } + p { "A small paragraph." } + h2 { "Heading 2" } + p { b { "Bold" } + " " + i { "Italic" } } } } I like the syntax of this very much, but I would like input on two things. The first this would require heavy use of instance_eval. Would this be a good thing? It strikes me that the main problem with instance_eval is that you can begin to change the interface of the class. However, as this is implemented above you can see that changes to the implementation would only effect an instance of the HtmlXX class, which would go into never never land as soon as the render call is completed. The second, a bit harder, I would like to remove the need for the pluses in order to chain the results together. Any Ruby gurus that would like to try and take a stab at it? I can think of one possible solution, but I'm not sure if the results would really be satisfactory. -- John Long contact me through: www.wiseheartdesign.com