2

Why does this produce the desired output (a list of team names, separated by commas):

<% teams.each_with_index do |t,i| -%><%= ',' unless i == 0  -%>
    <%= link_to t.name, team_path(t) -%>
<%- end %>

Output: One, Two, Three

And this not:

<% teams.each_with_index do |t,i| -%>
    <%= ',' unless i == 0  -%>
    <%= link_to t.name, team_path(t) -%>
<%- end %>

Output: One , Two , Three

My understanding is that the "-%>" should suppress the space before the comma. But clearly my understanding (or Rails 4.2.0) is wrong.

MZB
  • 2,071
  • 3
  • 21
  • 38
  • `-%>` behaves differently if using 'erb' vs 'erubis' libraries (ruby vs rails). https://stackoverflow.com/questions/7996695/what-is-the-difference-between-and-in-erb-in-rails – spuder Dec 23 '21 at 19:13

1 Answers1

4

No, if trim_mode is -, then ERB omit blank lines ending in -%>.

Look at the first code and its output :

require 'erb'

erb = ERB.new <<_, 0, '-'
<% teams.each_with_index do |t,i| -%>
<%= ',' unless i == 0  %> # here I have removed `-`
<%= t -%>
<%- end %>
_

teams = %w( India USA Brazil )
puts erb.result binding
# >> 
# >> India,
# >> USA,
# >> Brazil

Now the look at the effect of the - in -%> from the below code :

require 'erb'

erb = ERB.new <<_, 0, '-'
<% teams.each_with_index do |t,i| -%>
<%= ',' unless i == 0  -%>
<%= t -%>
<%- end %>
_

teams = %w( India USA Brazil )
puts erb.result binding
# >> India,USA,Brazil

And,

require 'erb'

erb = ERB.new <<_, 0, '-'
<% teams.each_with_index do |t,i| -%> <%= ',' unless i == 0  -%>
<%= t -%>
<%- end %>
_

teams = %w( India USA Brazil ) 
puts erb.result binding
# >>  India ,USA ,Brazil

I don't think there is anything from ERB side to strip out the white spaces. One way to get rid of this is adjusting the ERB template itself.

require 'erb'

erb = ERB.new <<_, 0, '-'
<% teams.each_with_index do |t,i| -%><%= ',' unless i == 0  -%>
<%= t -%>
<%- end %>
_

teams = %w( India USA Brazil )
puts erb.result binding
# >> India,USA,Brazil

Railish way is :

<%= teams.map { |t| link_to t.name, team_path(t) }.join(', ').html_safe %>

Here is some reasoning about the first code :

<% teams.each_with_index do |t,i| -%> will be deleted as you added - for each iteration. Now the next is <%= ',' unless i == 0 -%>, which will not be there for the fist time, but next iterations onward. But every time , will come without trailing space, as its previous erb tag getting deleted by -.

Here is some reasoning about the second code :

<% teams.each_with_index do |t,i| -%> will be deleted as you added - for each iteration. Now the next line is <%= ',' unless i == 0 -%>, which will not be there for the fist time, but next iterations onward. Now this has some extra indentation space, which is causing a space before every ,. [single space],

Arup Rakshit
  • 116,827
  • 30
  • 260
  • 317
  • 1
    And the reason that the newlines don't come through in the second example is because HTML ignores newlines in text. – Beartech Mar 03 '15 at 16:57
  • @Beartech I agree with you – Arup Rakshit Mar 03 '15 at 16:57
  • 1
    Excellent answer - thanks for adding the rails way which was what I really needed. It's a pity ERB doesn't have a mechanism to suppress leading white space before a <%=. – MZB Mar 03 '15 at 20:49