views:

40

answers:

1

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?

+1  A: 

<%= loginh -%> is almost what you want.

The trick is the minus sign in the closing part, which suppresses extra whitespace.

Alternatively, you could pipe the output through HTMLTidy using backticks (the ` character).

Daniel Heath
Interestingly enough I installed the Haml gem (alternate templating engine) and rewrote the view in Haml quickly.The indentation works just fine using Haml, and the helper code hasn't changed at all.Does Haml clean up the markup using Tidy? If it doesn't, I would suspect it would have the same problem as the ERb templating engine, no?
Robbie