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

From: takashikkbn@...
Date: 2019-01-24 12:37:00 UTC
List: ruby-core #91250
Issue #15538 has been updated by k0kubun (Takashi Kokubun).


Most of "foo"-"bar" examples seem to be indented strangely. I guess you meant the following one for all like this ("f" and "b" start with the same column), right?

```
3: foo
   bar
```

Now, you introduced 4 different new tokens (do not assume "<%|-" is transparently supported when "<%|" is supported):

* `<|`
* `<|-`
* `<|=`
* `|>`

Thank you for providing real examples (it's nice), but also,

* Could you summarize the effect of each token? 
* I'm still not understanding why 4 new tokens are needed. Can you eliminate 1 or 2 from them?

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

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

Instead of the invalid YAML:

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

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) %>
```





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