XML Builder - why?

I see that a lot of Rubyists like using XML Builder to generate XML.
why took this even further with markaby to generate HTML.

My question is: why? In what way is Builder any easier, clearer, more
succint than simpling using an ERB template? I find that Builder is
about the same length, with about the same structure, only introducing
an extra conversion. For what?

The reason I’m asking is that I see that a lot of bright, experienced
Ruby programmers use it, and I’m wondering if I’m missing something or
not.

[email protected] wrote:

I see that a lot of Rubyists like using XML Builder to generate XML.
why took this even further with markaby to generate HTML.

My question is: why? In what way is Builder any easier, clearer, more
succint than simpling using an ERB template? I find that Builder is
about the same length, with about the same structure, only introducing
an extra conversion. For what?

Uses plain Ruby. No ‘<’ '>" thingies. Automagic encoding of special
characters. Less chance of error. Focus on structure and logic, not
incidentals of the markup format.

Sure, for smallish things it may be overkill, but otherwise it is quite
handy, especially for the angle-bracket-phobic

The reason I’m asking is that I see that a lot of bright, experienced
Ruby programmers use it, and I’m wondering if I’m missing something or
not.

Perhaps not. Season to taste.


James B.

http://www.ruby-doc.org - Ruby Help & Documentation
Ruby Code & Style - The Journal By & For Rubyists
http://www.rubystuff.com - The Ruby Store for Ruby Stuff
http://www.jamesbritt.com - Playing with Better Toys
http://www.30secondrule.com - Building Better Tools

On Thu, Feb 16, 2006 at 03:33:27PM +0900, [email protected]
wrote:

I see that a lot of Rubyists like using XML Builder to generate XML.
why took this even further with markaby to generate HTML.

My question is: why? In what way is Builder any easier, clearer, more
succint than simpling using an ERB template? I find that Builder is
about the same length, with about the same structure, only introducing
an extra conversion. For what?

I like that the ruby parser checks the well-formedness of my markup for
me.

dave

Quoting [email protected]:

The reason I’m asking is that I see that a lot of bright,
experienced Ruby programmers use it, and I’m wondering if I’m
missing something or not.

Perhaps because it’s straight Ruby code – you can apply the same
sorts of transformations/refactorings to your templates as you can
the rest of your coe.

-mental

On Thursday 16 February 2006 1:33 am, [email protected] wrote:

I see that a lot of Rubyists like using XML Builder to generate XML.
why took this even further with markaby to generate HTML.

Well, I don’t really use Builder for HTML, but generate quite a lot of
XML with it. Here’s one simple use I like (make XSLT search-and-replace
with simple YAML):

#!/usr/bin/env ruby
require ‘yaml’
require ‘rubygems’
require_gem ‘builder’, ‘~> 1.2’

ARGV.each do |arg|
yml = YAML::load(File.open(arg))

b = Builder::XmlMarkup.new(:target => STDOUT,
:indent => 2)
b.xsl :stylesheet, “version” =>“1.0”,
“xmlns:xsl”
=>“XSLT Namespace” do
b.xsl :output, “method” =>“xml”, “encoding”=>“ascii”,
“cdata-section-elements=>”_facet"

b.comment!("Default Rule")
b.xsl :template, "match"=>"@*|node()" do
  b.xsl :copy do
    b.xsl :"apply-templates", "select"=>"@*|node()"
  end # xsl:template
end  # xsl:copy

yml.each { |outkey, outvalue|
  outvalue.each { |key, value|
    b.xsl :template, "match"=>"#{outkey}[. =

"`#{key}'"]" do
b.xsl :copy, “`#{value}'”
# b.xsl :message, “Matched #{key}”
end
} # end of outvalue.each
} # end of yaml.each
end
end

On Fri, 2006-02-17 at 02:50 +0900, [email protected] wrote:

Quoting [email protected]:

The reason I’m asking is that I see that a lot of bright,
experienced Ruby programmers use it, and I’m wondering if I’m
missing something or not.

Perhaps because it’s straight Ruby code – you can apply the same
sorts of transformations/refactorings to your templates as you can
the rest of your coe.

Personally I use the two in different situations. Generally I prefer to
just use ERb – I’m not especially afraid of angle brackets or anything
like that, however, in some circumstances Builder is so much more
sensible.

For example, I’m working on a Rails application at the moment which has
a particular item of data organised as a tree structure. To display this
tree structure I obviously need to use a recursive method. At first I
tried to do this with just plain HTML code, but it soon got very messy
with strings all over the place. I also wanted indentation so the
generated source code was readable, which meant passing an indentation
width to the recursive function as an argument… it was all very messy.
Anyway, I refactored this with Builder, which handled the indentation
for me and made the code a LOT more clean and readable because I no
longer had strings and angle brackets left right and centre.

I’d say the above is a very logical use of Builder, although I would
agree with you that using it for a WHOLE HTML page is going a little
too far. I guess at the end of the day though it comes down to
preference.

Also, it goes without saying that for true XML pages (as opposed to
HTML), Builder is far, far easier as it helps you keep pages well-formed
and neatly laid out.