Indentation in ERB Patterns

I have the following entry in the erb template:

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

This is a parsing:

 # Lorem Ipsum... zaz.property value 

How to remove leading spaces so that lines are not indented in the allowed pattern?

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 %> 
+5
source share
2 answers

The only solution I can offer is the hacker add <%- 'whatever here' %> before writing <%= %> :

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

output to irb

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

Rails doc claims that the 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 the - mode - .

+1
source

You can backtrack instead of ERB tags:

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

Source: https://habr.com/ru/post/1275578/


All Articles