Getting proper indentation using XMLBuilder in a helper

Posted by Robbie on Stack Overflow See other posts from Stack Overflow or by Robbie
Published on 2010-05-11T04:51:06Z Indexed on 2010/05/11 4:54 UTC
Read the original article Hit count: 336

Filed under:
|
|

I'm attempting to use a view helper to create some dynamic links based on if you're logged in or not.

What I want returned, for sake of easy code readability, is:

<ul class="right">
  <li><a href="#">Login</a></li>
  <li><a href="#">Register</a></li>
</ul>

In the view helper I have this Ruby code:

def loginh
  xm = Builder::XmlMarkup.new(:indent=>2, :margin=>4)
  xm.ul("class" => "right") {
     xm.li('class' => 'text') { 
        xm.text("test") 
     }
  }
end

In the view, the line that calls login helper is already indented 4 levels. Because of this, the first line gets 'skewed', so in the view I have:

        <%= loginh %>

Which results in:

                <ul class="right"> 
      <li class="text"> 
        <text>test</text> 
      </li> 
    </ul> 

You can see it works perfectly, except for the first line. It would appear that the first line is affected by the indent before <%= loginh %> is called.

I can easily remedy this by removing the indentation prior to <%= loginh %> - but in essence I'd be sacrificing code readability for markup readability. Which isn't what I'm looking to do.

Is there any way I could remove the beginning whitespace?

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about XHTML