Forum: Ruby XML Builder - why?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
unknown (Guest)
on 2006-02-16 08:33
(Received via mailing list)
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.
James B. (Guest)
on 2006-02-16 09:49
(Received via mailing list)
removed_email_address@domain.invalid 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
http://www.artima.com/rubycs/ - 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
David H. (Guest)
on 2006-02-16 15:24
(Received via mailing list)
On Thu, Feb 16, 2006 at 03:33:27PM +0900, removed_email_address@domain.invalid
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
Keith F. (Guest)
on 2006-02-16 15:42
(Received via mailing list)
On Thursday 16 February 2006 1:33 am, removed_email_address@domain.invalid 
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"
=>"http://www.w3.org/1999/XSL/Transform" 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}[. =
&quot;&#96;#{key}&apos;&quot;]" do
          b.xsl :copy, "`#{value}'"
          # b.xsl :message, "Matched #{key}"
        end
      } # end of outvalue.each
    } # end of yaml.each
  end
end
unknown (Guest)
on 2006-02-16 19:50
(Received via mailing list)
Quoting removed_email_address@domain.invalid:

> 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
Jonathan L. (Guest)
on 2006-02-16 20:51
(Received via mailing list)
On Fri, 2006-02-17 at 02:50 +0900, removed_email_address@domain.invalid wrote:
> Quoting removed_email_address@domain.invalid:
>
> > 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.
This topic is locked and can not be replied to.