Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Indentation in ERB templates

I have the following entry in an erb template:

# Lorem Ipsum...
<% unless @foo['bar'] == nil %>
    <% @foo['bar'].each do |property, value| %>
        <%= "zaz.#{property} #{value}" %>
    <% end %>
<% end %>

That is parsed to:

# Lorem Ipsum...
       zaz.property value

How can I remove the leading spaces so that lines are not indented in the resolved template?

I would like to avoid using something like:

# Lorem Ipsum...
<% unless @foo['bar'] == nil %>
    <% @foo['bar'].each do |property, value| %>
<%= "zaz.#{property} #{value}" %>
    <% end %>
<% end %>
like image 614
Navarro Avatar asked Feb 26 '18 10:02

Navarro


1 Answers

The only solution I can offer is hackish adding <%- 'whatever here' %> before the <%= %> entry:

<% [1,2,3].each do |f| %>
    <%- 1 %><%= f %>
<% end %>

it outputs in irb

irb(main):018:0> ERB.new(File.read('f.txt'), nil, '-').result
=> "\n1\n\n2\n\n3\n\n"

Rails doc claims, that default value for ERB trim_mode is - http://edgeguides.rubyonrails.org/configuring.html#configuring-action-view

And according to https://www.systutorials.com/docs/linux/man/1-erb/ ERB should remove spaces before <%- when - mode is enabled.

like image 99
Stanislav Mekhonoshin Avatar answered Oct 08 '22 18:10

Stanislav Mekhonoshin