From: Chris Newman Date: 2005-10-29T03:06:07+09:00 Subject: Re: newbie html question Thanks for your input! -----Original Message----- From: Ezra Zygmuntowicz [mailto:ezra@yakimaherald.com] Sent: Friday, October 28, 2005 12:48 PM To: ruby-talk@ruby-lang.org Subject: Re: newbie html question On Oct 28, 2005, at 5:14 AM, Chris Newman wrote: > Hi, > > I'm just starting to learn ruby and would like to know the best > library to > use to create a static html page from a cron job. The page will > contain the > results of a database query displayed as an html table. Some sort of > template package might be best. I already have dbi working to do > the query. > > Any advice would be appreciated. > Hey there- Here is an example of generating html with erb in the stdlib docs: Ruby in HTML ERB is often used in .rhtml files (HTML with embedded Ruby). Notice the need in this example to provide a special binding when the template is run, so that the instance variables in the Product object can be resolved. require "erb" # Build template data class. class Product def initialize( code, name, desc, cost ) @code = code @name = name @desc = desc @cost = cost @features = [ ] end def add_feature( feature ) @features << feature end # Support templating of member data. def get_binding binding end # ... end # Create template. template = %{ Ruby Toys -- <%= @name %>

<%= @name %> (<%= @code %>)

<%= @desc %>

<% if @cost < 10 %> Only <%= @cost %>!!! <% else %> Call for a price, today! <% end %>

}.gsub(/^ /, '') rhtml = ERB.new(template) # Set up template data. toy = Product.new( "TZ-1002", "Rubysapien", "Geek's Best Friend! Responds to Ruby commands...", 999.95 ) toy.add_feature("Listens for verbal commands in the Ruby language!") toy.add_feature("Ignores Perl, Java, and all C variants.") toy.add_feature("Karate-Chop Action!!!") toy.add_feature("Matz signature on left leg.") toy.add_feature("Gem studded eyes... Rubies, of course!") # Produce result. rhtml.run(toy.get_binding) Generates (some blank lines removed): Ruby Toys -- Rubysapien

Rubysapien (TZ-1002)

Geek's Best Friend! Responds to Ruby commands...

Call for a price, today!

Cheers- -Ezra Zygmuntowicz Yakima Herald-Republic WebMaster http://yakimaherald.com 509-577-7732 ezra@yakima-herald.com