Unindent ERB output

Hi all,

I'd like to unindent a block of ERB specifically to combat the extra
spacing being added to content inside <textarea> by the browser. Is
there such a feature in ERB?

I shall denote indentation with underscores in the pseudo code example
below.

Thanks in advance,
Khoan.

myview.erb:

<html ...>
__<%= render 'form' %>
</html>

_form.erb:

<% form_for ... do %>
__<%= render 'unindented' %>
<% end %>

_unindented.erb:

<textarea><%= 'this is unindented' %></textarea>

which would output something along the line of:

<html ...>
__<form ...>
____<textarea>this is unindented</textarea>
__</form>
</html>

I want something to this effect:

<html ...>
__<form ...>
<textarea>this is unindented</textarea>
__</form>
</html>

I don't think so, have you tried it and checked the html (View > Page
Source or similar in the browser). The line
__<%= render 'unindented' %>
says output underlines then render unindented. There is no way that
you can remove the underlines before the <%= render %>. However why
not just put, at the start of the line
<%= render 'unindented' %>
However I do not understand why it matters. None of this will give
you extra space *inside* the textarea.

Colin

I don’t think so, have you tried it and checked the html (View > Page

Source or similar in the browser). The line

__<%= render ‘unindented’ %>

says output underlines then render unindented. There is no way that

you can remove the underlines before the <%= render %>. However why

not just put, at the start of the line

<%= render ‘unindented’ %>

However I do not understand why it matters. None of this will give

you extra space inside the textarea.

Colin

Colin, i’m just clarifying what the OP meant. He replaced the tabs/spaces with underscores so it would be easy to picture.
He wanted to remove the indentation for the textarea tag so it would be in line with the html tag when you look at the

html source.

To the OP, I don’t think there’s an erb option that would give you what you want.

To solve the indentation problem, don't indent the tags in your
templates. You may have to experiment, but the result should be what
you want.

With ERB I've found that I can have either nicely aligned templates,
or somewhat nicely aligned HTML output. I write "somewhat" because it
never seems to be quite right, and the whole thing seems ugly and
awkward (surprising in Rails-land).

If anyone has a solution for this problem (besides Haml), please
respond. :slight_smile:

The other common issue is empty lines between code blocks. With Erubis
you can fix this by changing closing tags from "%>" to "-%>".

I Knew that, I also was showing underscores for spaces. The point is
that only spaces that the op includes in the erb will be copied
through to the html.

Colin

Thank you. I suspect as much. Would this be a worthwhile or nice to
have feature?

I'm using Rails 3. Erubis is using some XML::Builder with indent set
to 2.

I suspect Haml won't be of help either, since it'll delegate to Erubis
at some point.

the <%- only unindents a block to align with its closests <%= render
parent. Can't go further than that.

I don't understand why you are worried about additional whitespace in
the html. It should not make any difference to what is displayed.
Can you show an exact example of the problem html and explain why it
matters?

Colin

I have tried:

_form.erb:

<% form_for ... do %>
__<%- safe_concat '<textarea>this is unindented</textarea> %>
<% end %>

which would output:

<html ...>
__<form ...>
__<textarea>this is unindented</textarea>
__</form>
</html>

I have tried:

_form.erb:

<% form_for ... do %>
__<%- safe_concat '<textarea>this is unindented</textarea> %>
<% end %>

which would output:

<html ...>
__<form ...>
__<textarea>this is unindented</textarea>
__</form>
</html>

The question is why that matters to you. On screen or print it should
look exactly the same as
<html ...>
<form ...>
<textarea>this is unindented</textarea>
</form>
</html>

<html>
<body>
<textarea>if indent then
__indent
end</textarea>

__<textarea>if indent then
__indent
end</textarea>

</body>
</html>

When viewed in the browser:

the first <textarea> appears as and will be saved as thus:

if indent then
__indent
end

similarly, 2nd <textarea> appears as and will be saved as thus:

__if indent then
____indent
__end

OK, that is not the problem you originally asked. From your first
message you have
<textarea><%= 'this is unindented' %></textarea>
which is not the above situation at all.

Can you show me the erb you are using to generate the problem code you
are now showing?

Colin

The app is using formtastic to generate the textarea. I've watered it
down to the barebone code provided in the first post.

If we can unindent the <%= render 'unindent' %> to align with <html>,
then we have a winner. So far I fathom that this is not possible. Work
around would be to apply some js solution on the <textarea> to keep
the formatting we want.

The app is using formtastic to generate the textarea. I've watered it
down to the barebone code provided in the first post.

How are you giving formtastic the initial text for the textarea? Can
you show the code you use for that?

Please don't top post, it makes it difficult to follow the thread.
Insert your reply into the previous post. Thanks.

Colin

<%= semantic_form_for @template do |f| %>
  <%= f.input :source %>
<% end %>

where template is an instance of

class Template < ActiveRecord::Base
  # add_column :templates, :source, :text
end

<%= semantic_form_for @template do |f| %>
<%= f.input :source %>
<% end %>

where template is an instance of

class Template < ActiveRecord::Base
# add_column :templates, :source, :text
end

Please don't top post, it makes it difficult to follow the thread.
Insert your reply into the previous post. Thanks.

And what html does that generate (open it in the browser and use View

Page Source or similar to view the html, then copy the relevant

section and paste it here)?

Colin

I've solved the problem like this:

  https://github.com/tpo/irwi/commit/bf60ccf7a078f6a6fe971c245687915326b5cf60

(for context of this specific blossoming of this problem see:
https://github.com/alno/irwi/issues/19 )

I admit it's very ugly, however it works (it's the same way HAML seems
to solve this problem).

I'm left wondering how anybody was ever able to use f.text_area in
RoR...
*t