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],