Trimming whitespace from ERB

Discussion in 'Ruby' started by Phrogz, Sep 27, 2005.

  1. Phrogz

    Phrogz Guest

    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
    Phrogz, Sep 27, 2005
    #1
    1. Advertising

  2. On Sep 27 2005, 7:55 am, "Phrogz" <> 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.

    > 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
    Daniel Berger, Mar 7, 2007
    #2
    1. Advertising

  3. Phrogz

    Jan Svitok Guest

    On 3/7/07, Daniel Berger <> wrote:
    > On Sep 27 2005, 7:55 am, "Phrogz" <> 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
    Jan Svitok, Mar 8, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Oli Filth
    Replies:
    9
    Views:
    3,319
    Uncle Pirate
    Jan 17, 2005
  2. Replies:
    10
    Views:
    722
    Eric Brunel
    Dec 16, 2008
  3. MRAB
    Replies:
    3
    Views:
    373
  4. Steve V
    Replies:
    6
    Views:
    234
    Steve V
    Apr 20, 2005
  5. Dan Thomas

    link to .erb file, from .erb file

    Dan Thomas, Jan 26, 2011, in forum: Ruby
    Replies:
    4
    Views:
    159
    Dan Thomas
    Jan 26, 2011
Loading...

Share This Page