From: Philipp Meier Date: 2002-11-09T21:12:57+09:00 Subject: Re: An alternative HTML generation syntax Am Sam, 2002-11-09 um 09.47 schrieb Philip Mak: > It would be this: > > HTML.new { p { > t 'NEW!' if flag == 'new' > t 'UPDATED!' if flag == 'updated' > br > if numparts > 1 > t 'Parts:' > (1..numparts).each do |i| > a(:href => "#{i}.asp") { "[#{i}]" } > end > end > } } > > The HTML class would be something like this: > > class HTML > def initialize(&block) > @buff = '' > self.instance_eval &block Why not yield? > puts @buff > end > > def p > @buff += "

#{yield || @buff}

" > end > # define a, h1, h2, etc. and all the other HTML tags similarly > end This should certainly work. I'd suggest using method_missing and have a class local configuration which says elements html allows and which attributes are needed. Sth. like i micro dtd: class HTML @@dtd = { :html => {}, :head => {}, :title => {}, :body => {:bgcolor, :link, :vlink, :alink} } ... def methods_missing(symbol, *attrs) raise "Invalid element: #{symbol}" unless @@dtd.has_key? symbol @buff << "<#{symbol}>#{yield || @buff}" end end You can go even further and use a DTD parser to decouple this nifty tool from HTML. Make it a Provider of a SAX Stream, e.g. -billy. -- Meisterbohne S�flinger Stra�e 100 Tel: +49-731-399 499-0 eL�sungen 89077 Ulm Fax: +49-731-399 499-9