Feature #15538
Updated by kke (Kimmo Lehto) almost 6 years ago
In `Erb`, would it be possible to add a new trim mode that would indent the following content to match the depth of the tag? The tag could for example be `<%|` and it would be enabled using `Erb.new(template, trim_mode: '|')` ## Reason Something like this would be easy to follow: looks nice: ``` ruby 1 - horse <%- [2, 3, 4].each <% ['cat', 'dog', 'goldfish'].each do |num| -%> |pet| %> <%- <% unless num pet == 3 -%> 'goldfish' %> - <%= num pet %> <%- <% end -%> %> <%- <% end -%> %> 5 ``` But unfortunately it will render with "extra" indentation: ``` 1 2 4 - horse - cat - dog 5 ``` Currently, to avoid this, you have to write your template using either no indentation: ``` 1 <%- [2, 3, 4].each do |num| -%> <%- unless num == 3 -%> <%= num %> <%- end -%> <%- end -%> 5 ``` Or a weird jumpy indentation: ``` 1 <%- [2, 3, 4].each do |num| -%> <%- unless num == 3 -%> <%= num %> <%- end -%> <%- end -%> 5 ``` With the `|` trim mode it could be written as: ```ruby 1 - horse <%| [2, 3, 4].each ['cat', 'dog', 'goldfish'].each do |num| -%> <%- unless num == 3 -%> <%= num |pet| %> <%- end -%> <%- .... <% end -%> %> 5 ``` And it would output as desired without the "extra" indentation: indentation. Currently, to produce such output, you have write your erb template without indentation or using a jumpy mixed indentation (code and content have different indentation levels but are interleaved) and it's not a very user/editor friendly way to work. ``` 1 2 4 5 ``` ## Using with `=` It would also be handy if the `|` could be used with `<%=`, perhaps `<%|=`, this would be excellent for example when templating YAML's: ```yaml <%- <% bar_content = "- abc\n- " -def" -%> something\n- something else" %> foo: bar: <%|= bar_content %> ``` Which would produce something like: ```yaml foo: bar: - abc something - def something else ``` Instead of the invalid YAML: invalid: ```yaml foo: bar: - abc something - def something else ``` Currently there's no way to accomplish this without manually counting the leading spaces and using something like: ```yaml <%- bar_content = "- abc\n- " -def" -%> foo: bar: <%= bar_content.gsub(/(?<!\A)^(?!$)/m, ' ' * 4 %> ``` spaces, as it's not possible to detect the column where `<%=` appeared.