ERB模板中的缩进

时间:2018-02-26 10:06:03

标签: ruby-on-rails ruby erb

我在socket.io模板中有以下条目:

erb

将其解析为:

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

如何删除前导空格,以便在已解析的模板中不会缩进行?

我想避免使用类似的东西:

# Lorem Ipsum...
       zaz.property value

1 个答案:

答案 0 :(得分:2)

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.