Feature #15538
closedErb indenting / unindenting
Description
In Erb, would it be possible to add a new tag that would indent the following content to match the depth of the tag? The tag could be <%~ (to resemble the <<~EOS squiggy heredoc).
Reason¶
Something like this would be easy to follow:
1
<%- [2, 3, 4].each do |num| -%>
<%- unless num == 3 -%>
<%= num %>
<%- end -%>
<%- end -%>
5
But unfortunately it will render with "extra" indentation:
1
2
4
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 <%~ it could be written as:
1
<%~ [2, 3, 4].each do |num| -%>
<%- unless num == 3 -%>
<%= num %>
<%- end -%>
<%~ end -%>
5
And it would output as desired without the "extra" indentation:
1
2
4
5
Another example:
<%= "abcd" %> <%~ [1.2.3].each do |num| -%>
<%= num %>
<%~ end -%>
would produce:
abcd 1
2
3
Using with =
¶
It would also be handy if the ~ could be used in <%= statements, perhaps as <%~=. This would be excellent for example when templating YAML's:
<%- bars = %w(abc def)" -%>
foo:
bar:
<%~= bars.map { |bar| "- #{bar}\n" } %>
Which would reindent the statement outcome to produce something like:
foo:
bar:
- abc
- def
This would require these new tags:
-
<%~begin a code block and begin or end reindentation mode. content produced inside the block will be reindented to the depth of the<character in<%~. If the indentation mode was already active due to a previous<%~, it ends the indentation mode. -
<%~=like regular<%=but multiline strings will be reindented to the column of the<character