Trimming whitespace from ERB

Summary:
a) Is there a way to ask ERB to trim leading whitespace between the
start-of-line and a '<%'? (The equivalent of .gsub( /^\s+<%/, '<%' ) If
not, I suggest that one should exist.

b) What is up with the crazy syntax for the third param to ERB? Could
we perhaps come up with a cleaner, less-ambiguous way to specify
various parsing options? Hash options? One char per flag?

Details:
See the templates below. I want to output a certain text string
indented by a specific number of tabs, with a single line break at the
end. ERB does not seem to have a setting to ignore leading whitespace
for an opening <% tag. This means that I have to either manage the
whitespace manually (using _erbout) or play with where I put the <% and
%> tags (making the code surprisingly hard to read).

require 'erb'

desired_but_unusable_template = <<ENDTEMPLATE
Hello
<%
  4.times{
    %>
    BLING
    <%
  }
%>
World
ENDTEMPLATE

ERB.new( desired_but_unusable_template ).run
#=> Hello
#=>
#=> BLING
#=>
#=> BLING
#=>
#=> BLING
#=>
#=> BLING
#=>
#=> World

ERB.new( desired_but_unusable_template, nil, '%>' ).run
#=> Hello
#=> BLING
#=> BLING
#=> BLING
#=> BLING
#=> World

gross_but_correct_template_1 = <<ENDTEMPLATE
Hello
<%
  4.times{
    %>
    BLING
<%
  }
%>
World
ENDTEMPLATE

ERB.new( gross_but_correct_template_1, nil, '%>' ).run
#=> Hello
#=> BLING
#=> BLING
#=> BLING
#=> BLING
#=> World

gross_but_correct_template_2 = <<ENDTEMPLATE
Hello<%
  4.times{
    %>
    BLING<%
  }
%>
World
ENDTEMPLATE

ERB.new( gross_but_correct_template_2 ).run
#=> Hello
#=> BLING
#=> BLING
#=> BLING
#=> BLING
#=> World

gross_but_correct_template_3 = <<ENDTEMPLATE
Hello
<%
  4.times{
    _erbout << "\t\t"
    %>BLING<%
    _erbout << "\n"
  }
%>
World
ENDTEMPLATE

ERB.new( gross_but_correct_template_3, nil, '%>' ).run
#=> Hello
#=> BLING
#=> BLING
#=> BLING
#=> BLING
#=> World

Summary:
a) Is there a way to ask ERB to trim leading whitespace between the
start-of-line and a '<%'? (The equivalent of .gsub( /^\s+<%/, '<%' ) If
not, I suggest that one should exist.

I second this.

b) What is up with the crazy syntax for the third param to ERB? Could
we perhaps come up with a cleaner, less-ambiguous way to specify
various parsing options? Hash options? One char per flag?

And this one, too.

Anyone got a patch?

Details:
See the templates below. I want to output a certain text string
indented by a specific number of tabs, with a single line break at the
end. ERB does not seem to have a setting to ignore leading whitespace
for an opening <% tag. This means that I have to either manage the
whitespace manually (using _erbout) or play with where I put the <% and
%> tags (making the code surprisingly hard to read).

require 'erb'

desired_but_unusable_template = <<ENDTEMPLATE
Hello
<%
        4.times{
                %>
                BLING
                <%
        }
%>
World
ENDTEMPLATE

ERB.new( desired_but_unusable_template ).run
#=> Hello
#=>
#=> BLING
#=>
#=> BLING
#=>
#=> BLING
#=>
#=> BLING
#=>
#=> World

ERB.new( desired_but_unusable_template, nil, '%>' ).run
#=> Hello
#=> BLING
#=> BLING
#=> BLING
#=> BLING
#=> World

gross_but_correct_template_1 = <<ENDTEMPLATE
Hello
<%
        4.times{
                %>
                BLING
<%
        }
%>
World
ENDTEMPLATE

ERB.new( gross_but_correct_template_1, nil, '%>' ).run
#=> Hello
#=> BLING
#=> BLING
#=> BLING
#=> BLING
#=> World

gross_but_correct_template_2 = <<ENDTEMPLATE
Hello<%
        4.times{
                %>
                BLING<%
        }
%>
World
ENDTEMPLATE

ERB.new( gross_but_correct_template_2 ).run
#=> Hello
#=> BLING
#=> BLING
#=> BLING
#=> BLING
#=> World

gross_but_correct_template_3 = <<ENDTEMPLATE
Hello
<%
        4.times{
                _erbout << "\t\t"
                %>BLING<%
                _erbout << "\n"
        }
%>
World
ENDTEMPLATE

ERB.new( gross_but_correct_template_3, nil, '%>' ).run
#=> Hello
#=> BLING
#=> BLING
#=> BLING
#=> BLING
#=> World

Regards,

Dan

···

On Sep 27 2005, 7:55 am, "Phrogz" <g...@refinery.com> wrote:

> Summary:
> a) Is there a way to ask ERB to trim leading whitespace between the
> start-of-line and a '<%'? (The equivalent of .gsub( /^\s+<%/, '<%' ) If
> not, I suggest that one should exist.

I second this.

Reading the source revealed this: There is '-' ('explicit trim') mode,
that allows this behaviour. You need to use <%- and -%> wherever you
want to have whitespace stripped (whitespace = spaces & tabs before,
newline after)

This mode is undocumented (anyone patch?).

> b) What is up with the crazy syntax for the third param to ERB? Could
> we perhaps come up with a cleaner, less-ambiguous way to specify
> various parsing options? Hash options? One char per flag?

And this one, too.

Mode can be specified either as a string ('-', '%-', '>', '%>', '<>',
'%<>') or a number (0 = '', 1 = '>', 2 = '<>'). Maybe there should be
or-flags like:

PERCENT_LINES | TRIM_NEWLINE_BEFORE | TRIM_NEWLINE_AFTER |
TRIM_WHITESPACE_BEFORE

with shortcuts like

PERCENT_AND_TRIM_NEWLINES ~ '%<>'

Maybe for a start a few constants naming the various modes would be nice

It shouldn't be that hard... look for prepare_trim_mode and then for
the various scanners.

-----------------------------------8<-----------------

require 'erb'

desired_template = <<ENDTEMPLATE
Hello
<%-
     4.times{
             -%>
             BLING
             <%-
         }
-%>
World
ENDTEMPLATE

ERB.new( desired_template, nil, '-' ).run

···

On 3/7/07, Daniel Berger <djberg96@gmail.com> wrote:

On Sep 27 2005, 7:55 am, "Phrogz" <g...@refinery.com> wrote: