From: "Ara.T.Howard" Date: 2003-11-24T04:02:18+09:00 Subject: Re: HTML Generation (Next Generation CGI) On Sun, 23 Nov 2003, Mauricio Fern�ndez wrote: > Date: Sun, 23 Nov 2003 18:19:56 +0900 > From: Mauricio Fern�ndez > Newsgroups: comp.lang.ruby > Subject: Re: HTML Generation (Next Generation CGI) > > On Sun, Nov 23, 2003 at 01:50:29AM +0900, John W. Long wrote: > > Last night I started prototyping some of my ideas and came up with the > > following: > [...] > > 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" } } > > } > > } what advantage would this give over: ~ > cat bar.rb require 'amrita/template' template = Amrita::TemplateText.new <<-html

html data = Hash[ :title => 'My Document', :meta => Amrita::a(:name => 'description'){'this is a description of this document'}, :h1 => 'Heading 1', :p => 'A small paragraph', :h2 => 'Heading 2', :b => 'Bold', :i => 'Italic', ] template.expand STDOUT, data ~ > ruby bar.rb My Document

Heading 1

Heading 2

Bold Italic

> You can use a technique similar to flgr's Junction or oGMo's criteria to build a > "parse tree". > > What would be really cool would be taking a DTD and generating the Ruby > code from that that validates the document as it is built. > It would be possible to define how blocks can be nested, etc, in > practice, ensuring that no illegal sequence of calls is made. ~ > cat foo.rb require 'amrita/template' template = Amrita::TemplateText.new <<-html
html data = Hash.new[ :row => {:a => 'illegal', :b => 'row'} ] template.expand STDOUT, data ~ > ruby foo.rb /data/ruby-1.8.0//lib/ruby/site_ruby/1.8/amrita/parser.rb:304:in `parse': error hapend in :2( can't be in ) (Amrita::HtmlParseError) ==>> from /data/ruby-1.8.0//lib/ruby/site_ruby/1.8/amrita/parser.rb:269:in `parse_text' from /data/ruby-1.8.0//lib/ruby/site_ruby/1.8/amrita/template.rb:405:in `load_template' from /data/ruby-1.8.0//lib/ruby/site_ruby/1.8/amrita/template.rb:208:in `setup_template' from /data/ruby-1.8.0//lib/ruby/site_ruby/1.8/amrita/template.rb:115:in `expand' from foo.rb:10 amrita is pretty hard to beat. at the very lest, it could be built on. it's handling of array's make generating complex pages very easy: ~ > cat foobar.rb require 'amrita/template' template = Amrita::TemplateText.new <<-html
html data = Hash[ :rows => [ {:name => 'joe', :ssn => '574-86-3205'}, {:name => 'bob', :ssn => '572-84-8964'}, # imagine there are _many_ of these ] ] template.expand STDOUT, data ~ > ruby foobar.rb
joe574-86-3205
bob572-84-8964
it will hard to beat amrita's handling of nested data structures. another thing that is really good about it is that it separated html from cgi logic. that way to can say "don't like how it looks?, here's the template - add something" to your web designers. anyhow, amrita is worth checking out. -a -- ATTN: please update your address books with address below! =============================================================================== | EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov | PHONE :: 303.497.6469 | ADDRESS :: E/GC2 325 Broadway, Boulder, CO 80305-3328 | STP :: http://www.ngdc.noaa.gov/stp/ | NGDC :: http://www.ngdc.noaa.gov/ | NESDIS :: http://www.nesdis.noaa.gov/ | NOAA :: http://www.noaa.gov/ | US DOC :: http://www.commerce.gov/ | | The difference between art and science is that science is what we | understand well enough to explain to a computer. | Art is everything else. | -- Donald Knuth, "Discover" | | /bin/sh -c 'for l in ruby perl;do $l -e "print \"\x3a\x2d\x29\x0a\"";done' ===============================================================================