From: takashikkbn@... Date: 2019-01-16T12:55:01+00:00 Subject: [ruby-core:91125] [Ruby trunk Feature#15538] Erb indenting / unindenting trim mode Issue #15538 has been updated by k0kubun (Takashi Kokubun). Thanks for the update. You didn't answer 2 but I guess you're using `trim_mode: '-'` or something including "-". Note that your "extra indentation" result is still not valid one since it should have 4 spaces in the real world. Do not treat your example indentation as trivial while talking about the indentation feature. Now I'm kind of understanding what you want to achieve. But the details of what you want is not 100% clear to me, and with a real implementation your first example does not seem to work as you intended. So, let me ask questions for each feature. ## <%| For this, ```erb 1 <%| [2, 3, 4].each do |num| -%> <%- unless num == 3 -%> <%= num %> <%- end -%> <%- end -%> 5 ``` * When does the *de-indentation* end? * When ERB finds the second `end` matching `each do`? If so, as it's super hard to implement (note that ERB is NOT aware of Ruby expressions inside "<%"s, so you should give up detecting the "matching" `end`), please consider changing the syntax to the following one like Erubi's capture syntax (by having second "<%|", ERB will be able to know when it should finish de-indentation): ```erb 1 <%| [2, 3, 4].each do |num| -%> <%- unless num == 3 -%> <%= num %> <%- end -%> <%| end -%> 5 ``` * Assuming the above suggestion is accepted, what's the expected result of the following template? ```erb - foo <%| ['bar', 'baz'].each do |text| -%> - <%= text %> <%| end -%> ``` maybe this 2 spaces, not 0 space or 4 spaces? (because the first "<%|" is indented with 2 spaces, any indentation longer than 2 is shrunk to 2 spaces until it reaches the second "<%|") ```erb - foo - bar - bar ``` * What happens when non-space is in the same line before "<%|"? Can ERB raise an error for it? ``` foo <%| 3.times do |i| %> <%= i %> <%| end %> ``` * Could you consider not using "<%|" for this since Erubi is using it for capturing as @jeremyevans said? ## <%|= * Same here. Please consider using another tag literal. * What happens if "<%=" is placed inside "<%|" area? Indentation the same as "<%|" + extra indentation for "<%="? Can we make it an error because it's too hard to expect the behavior? ---------------------------------------- Feature #15538: Erb indenting / unindenting trim mode https://bugs.ruby-lang.org/issues/15538#change-76357 * 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(/(? ``` -- https://bugs.ruby-lang.org/ Unsubscribe: