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 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:

__<%= render 'form' %>

_form.erb:

<% form_for … do %>
__<%= render ‘unindented’ %>
<% end %>

_unindented.erb:

<%= ‘this is unindented’ %>

which would output something along the line of:

__ ____this is unindented __

I want something to this effect:

__ this is unindented __

On 17 February 2011 04:19, khoan [email protected] wrote:

Khoan.

which would output something along the line of:

__ ____this is unindented

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

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.

On 17 February 2011 11:26, Jim Ruther N. [email protected] wrote:

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.

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

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.

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

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 have tried:

_form.erb:

<% form_for … do %>
__<%- safe_concat 'this is unindented %>
<% end %>

which would output:

__ __this is unindented __

On 18 February 2011 11:13, khoan [email protected] wrote:

__ __this is unindented __

The question is why that matters to you. On screen or print it should
look exactly the same as

this is unindented

On 18 February 2011 11:09, khoan [email protected] wrote:

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

On 19 February 2011 09:30, khoan [email protected] wrote:

similarly, 2nd 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
<%= ‘this is unindented’ %>
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

if indent then __indent end

__if indent then
__indent
end

When viewed in the browser:

the first appears as and will be saved as thus:

if indent then
__indent
end

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

__if indent then
____indent
__end

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 ,
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 to keep
the formatting we want.

<%= 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

On 20 February 2011 08:39, khoan [email protected] wrote:

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

On 21 February 2011 08:51, khoan [email protected] wrote:

<%= 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

On 17 Feb., 06:19, khoan [email protected] wrote:

I’d like to unindent a block of ERB specifically to combat the extra
spacing being added to content inside by the browser.

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs