# Using partials with XML builder, what's the right way?

**URL:** <https://www.ruby-forum.com/t/using-partials-with-xml-builder-whats-the-right-way/189930>\
**Category:** Rails\
**Created:** [June 10, 2010, 3:59am UTC](https://www.ruby-forum.com/t/using-partials-with-xml-builder-whats-the-right-way/189930 "2010-06-10T03:59:11Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Matthew\_Hillsboroug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ac91a4/32.png) [@Matthew\_Hillsboroug](https://www.ruby-forum.com/u/Matthew_Hillsboroug)\
**Post date:** [June 10, 2010, 3:59am UTC](https://www.ruby-forum.com/t/using-partials-with-xml-builder-whats-the-right-way/189930/1 "2010-06-10T03:59:11Z")

</div>

Hello everyone,

Partials in XML builder are proving to be non-trivial.

After some initial Google searching, I found the following to work,  
although it’s not 100%

```
 xml.foo do
     xml.id(foo.id)
     xml.created_at(foo.created_at)
     xml.last_updated(foo.updated_at)
     foo.bars.each do |bar|
         xml << render(:partial => 'bar/_bar', :locals => { :bar

```

=\> bar })  
end  
end

this will do the trick, except the XML output is not properly  
indented. the output looks something similar to:

```
<foo>
  <id>1</id>
  <created_at>sometime</created_at>
  <last_updated>sometime</last_updated>
<bar>
  ...
</bar>
<bar>
  ...
</bar>
</foo>

```

The `<bar>` element should align underneath the `<last_updated>`  
element, it is a child of `<foo>` like this:

```
<foo>
  <id>1</id>
  <created_at>sometime</created_at>
  <last_updated>sometime</last_updated>
  <bar>
    ...
  </bar>
  <bar>
    ...
  </bar>
</foo>

```

Works great if I copy the content from bar/\_bar.xml.builder into the  
template, but then things just aren’t DRY.

Thanks,

Matthew

---

<div class="post-metadata">

**Author:** ![Matthew\_Hillsboroug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ac91a4/32.png) [@Matthew\_Hillsboroug](https://www.ruby-forum.com/u/Matthew_Hillsboroug)\
**Post date:** [June 10, 2010, 4:30am UTC](https://www.ruby-forum.com/t/using-partials-with-xml-builder-whats-the-right-way/189930/2 "2010-06-10T04:30:09Z")

</div>

Matthew Hillsborough wrote:

> Hello everyone,
> 
> Partials in XML builder are proving to be non-trivial.

So use Haml instead of Builder – it works just as well for XML as for  
HTML. Problem solved.

IMHO, Builder is the single most pointless thing in the Rails ecosystem.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![Matthew\_Hillsboroug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ac91a4/32.png) [@Matthew\_Hillsboroug](https://www.ruby-forum.com/u/Matthew_Hillsboroug)\
**Post date:** [June 10, 2010, 11:30am UTC](https://www.ruby-forum.com/t/using-partials-with-xml-builder-whats-the-right-way/189930/3 "2010-06-10T11:30:29Z")

</div>

I’ve swapped-in Nokogiri rather than builder, but my general strategy  
is the same; using helper methods to encapsulate discreet xml building  
code for reuse rather than partials. Seems less hackish. Just pass  
in the xml object you are building up.
