On Wed, 13 Oct 2004 22:21:27 +0900 Gavin Kistner <gavin / refinery.com> wrote: | On Oct 13, 2004, at 3:22 AM, David Heinemeier Hansson wrote: | > Yes, it's called ERb and is basically a Ruby-version of eRuby. It's | > even included in the standard library with Ruby 1.8.x. Have a look | > at http://www.ruby-doc.org/stdlib/libdoc/erb/rdoc/index.html | | Awesome. Thanks, David! | | Except...erm...there's no documentation. | | When I have more free time I'd love to help out the ruby-doc project | and document this library. But right now I'm working 12 hour days and | don't have time to dive into the source code to figure it out. | | Anyone have a bit of an example showing how to set up an HTML template | | with embedded ruby code, and then load that template into the program | code at a current state (so all the necessary variables are setup) and | | 'flatten' the template into the output HTML? | I don't know if the following helps, but I'm currently using ERb for one of my projects. The template does not use HTML but LaTeX, but it is the same for HTML anyway. --------------------- template snippet with ERb code fragments ----- \ecvlastname{<%= value('header/name/surname') %>} \ecvfirstname{<%= value('header/name/firstname') %>} \ecvaddress{<%= value('header/address') %>} <% if resume.elements['header/contact/phone'] %> \ecvtelephone{<%= value('header/contact/phone') %>} <% end %> <% if resume.elements['header/contact/fax'] %> \ecvfax{<%= value('header/contact/fax') %>} <% end %> <% if resume.elements['header/contact/email'] %> \ecvemail{\texttt{<%= value('header/contact/email') %>}} <% end %> \ecvnationality{Austrian} \ecvdateofbirth{<%= value('header/birth/date') %>} --------------------------------------------------------------------- ---------------------------- Converter class using ERb -------------- class Converter attr_writer :template attr_writer :source attr_writer :config def initialize @template = nil @source = nil @config = nil end def convert @processor = ResumeProcessor.new( @config ) # Create a new ERB object with the given template erb = ERB.new( File.read( @template ) ) file = File.new( @source ) @resume = REXML::Document.new( File.new( @source ) ).root resume = @resume # Evaluate the template in the current context print erb.run( binding ) end def value( path, element = @resume ) @processor.get_value( element.elements[path] ) end end c = Converter.new #... set template, source and config for converter c.convert --------------------------------------------------------------------- The @template variable for the Converter class is loaded with the filename of the template. You only have to call the erb.run method (and probably supply a binding) and it will return the result. Thomas