[ruby-core:91496] [Ruby trunk Feature#15538] Erb indenting / unindenting trim mode

From: takashikkbn@...
Date: 2019-02-08 16:05:10 UTC
List: ruby-core #91496
Issue #15538 has been updated by k0kubun (Takashi Kokubun).

Status changed from Feedback to Third Party's Issue

Thank you for the update.

As @jeremyevans said, the tags conflict with ones already used by Erubi. While you explained `<%|` as "capturing a block", I think what your `<%|` does in your examples seems to behave differently from Erubi's capture.

As your `<%|` is conflicting with a widely-used ERB-like gem Erubi, I don't think it's a good idea to introduce your suggestion as `<%|`. But you preferred `<%|` even after I said "Could you consider not using `<%|`". If we really need it, I think that should be achieved as an unofficial gem so that we do not break existing ecosystem globally.

So I prepared this for you https://github.com/k0kubun/erb-indent. Since your specification does not make a full sense to me, I did not implement full features of your suggestion. For example, why would we need `|%>` for the `<%|=` line? Only you can understand such kind of details, so please complete the implementation by yourself. It's already demonstrating how we could implement extra tag like `<%|` and only details of `<%|` features are missing. Thank you.

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

* Author: kke (Kimmo Lehto)
* Status: Third Party's Issue
* 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 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 `|`  trim mode 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
```

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

Which would produce something like:

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

This would require these new tags:

1. `<%|` begin a capturing a block which will be reindented to the column of the `<` character
2. `|%>` end the capturing block
3. `<%|=` like regular `<%=` but multiline strings will be reindented to the column of the `<` character

Optionally versions of the block tags with whitespace trim control:

4. `<%-|` like `<%|` but if block outputs nothing, remove leading whitespace
5. `|-%>`like `|%>` but remove trailing whitespace and linefeed from the line this tag appears on




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

In This Thread

Prev Next