Builder: The other template

Posted by tom March 15th, 2006

Builder is a library which RAILS will use if you end your templates in .rxml. As you can probably guess it’s very helpful in case you want to generate XML files.

The following code (from Agile Web Development with RAILS) will generate an XML file (see below):

xml.div(:class => “productlist”) do
  xml.timestamp(Time.now)
  @products.each do |product|
    xml.product do
      xml.productname(product.title)
      xml.price(product.price, :currency => “USD”)
    end
  end
end
As you see this is pretty convenient, if not ideal! This is the XML it generates:
<div class="productlist">
  <timestamp>Wed Mar 15 22:13:31 CET 2006</timestamp>
  <product>
    <productname>Pragmatic Programmer</productname>
    <price currency="EUR">39.96</price>
  </product>
  <product>
    <productname>Programming Ruby</productname>
    <price currency="EUR">44.95</price>
  </product>
</div>

Leave a Reply