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


> Perhaps <%~ would be good as it resembles the squiggly heredoc <<~EOB

Thanks to rethink that part. I feel it's a good idea to make it similar to `<<~`.

The proposal change makes your idea closer to be added to the ERB core, but we still need working code from you because the behaviors on full edge cases are hard to understand from the given examples. Let's try implementing that in erb-indent.gem (or a gem forked from it if you like) first so that we can try/use the feature without waiting for agreements and Ruby releases.

> It seems to only work when the <%| is in the beginning of the line, for anything else it fails. I have yet to find a way to find the index of stag, but I have a hunch it has to be done in the line scanner and yielded there.

Try override #make_scanner (and return a scanner inherited from existing one) if you need to change the scanner's behavior, like I did in #make_compiler.

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

* Author: kke (Kimmo Lehto)
* Status: Third Party's Issue
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
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:

``` ruby
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:

```ruby
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:

```yaml
<%- bars = %w(abc def)" -%>
foo:
  bar: 
    <%~= bars.map { |bar| "- #{bar}\n" } %>
```

Which would reindent the statement outcome to produce something like:

```yaml
foo:
  bar:
    - abc
    - def
```

This would require these new tags:

1. `<%~` 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.
2. `<%~=` like regular `<%=` but multiline strings will be reindented to the column of the `<` character





-- 
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>