Issue #15538 has been updated by k0kubun (Takashi Kokubun).

Status changed from Open to Feedback

It's hard to understand your proposal for me. Could you fix the following two parts first?

1. Do not use "...." in the template. We are talking about a feature to change indentation and the indentation you imagine in "...." is not clear to me.
2. Clarify the trim_mode you intended to use for each template, and make sure your template actually matches the result you show. At least your first template and its result does not seem to match in the default trim_mode.

```
$ cat a.rb
src = <<-EOS
- horse
<% ['cat', 'dog', 'goldfish'].each do |pet| %>
  <% unless pet == 'goldfish' %>
    - <%= pet %>
  <% end %>
<% end %>
EOS

puts ERB.new(src).result
$ ruby -rerb a.rb
- horse


    - cat



    - dog




```

----------------------------------------
Feature #15538: Erb indenting / unindenting trim mode
https://bugs.ruby-lang.org/issues/15538#change-76339

* Author: kke (Kimmo Lehto)
* Status: Feedback
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
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 looks nice:

``` ruby
- horse
<% ['cat', 'dog', 'goldfish'].each do |pet| %>
  <% unless pet == 'goldfish' %>
    - <%= pet %>
  <% end %>
<% end %>
```

But unfortunately it will render with "extra" indentation:

```
- horse
   - cat
   - dog
```

With the `|`  trim mode it could be written as:

```ruby
- horse
<%| ['cat', 'dog', 'goldfish'].each do |pet| %>
....
<% end %>
```

And it would output as desired without the "extra" 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.

## 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 = "- something\n- something else" %>
foo:
  bar: 
    <%|= bar_content %>
```

Which would produce something like:

```yaml
foo:
  bar:
    - something
    - something else
```

Instead of the invalid:

```yaml
foo:
  bar:
    - something
- something else
```

Currently there's no way to accomplish this without manually counting the leading spaces, as it's not possible to detect the column where `<%=` appeared.



-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>