Blank lines generated by ERB - Can we prevent them?

Discussion in 'Ruby' started by RichardOnRails, Mar 6, 2011.

  1. Hi,

    I'm running:
    Ruby 1.8.6
    eRuby 1.0.5
    WinXP-Pro/SP3

    I've got a three-line script:
    <%#- Example from -%>
    <%#- http://www.linuxtopia.org/online_bo...by_tutorial/Ruby_and_the_Web_Using_eruby.html
    -%>
    This text is <% a = 100; print "#{a}% Live!" %>
    === end of script ===

    that produces 5 lines of output in a Command Window (2 blank lines, a
    line of text and 2 more blank lines):
    K:\_Projects\Ruby\_Ruby_Tests\TestEruby>eruby Test_eruby_01.erb


    This text is 100% Live!


    K:\_Projects\Ruby\_Ruby_Tests\TestEruby>
    === end of relevant portion of Cmd Window ===

    I haven't been able to get rid of those blank lines. Is there a way?

    Thanks in advance,
    Richard
     
    RichardOnRails, Mar 6, 2011
    #1
    1. Advertising

  2. RichardOnRails wrote in post #985685:
    > I haven't been able to get rid of those blank lines. Is there a way?


    It looks like erb comments don't support the -%> way of preventing the
    blank lines. I tried both erb and eruby command-line tools.

    Regular ruby comments seem OK though:

    <% # Example from -%>
    <% # http://www.linuxtopia.org/online_books/programming... -%>
    This text is <% a = 100; print "#{a}% Live!" %>

    BTW it's bad practice to embed print inside erb (and you'll see the
    'erb' command line tool gives unexpected results, maybe other erb
    implementations will too). This is because you may find erb being
    assembled into a buffer and then printed at the end.

    Much better to use <%= ... %> to embed output. For example:

    This text is <% a = 100 %><%= "#{a}% Live!" %>

    or:

    This text is <%= a = 100; "#{a}% Live!" %>

    --
    Posted via http://www.ruby-forum.com/.
     
    Brian Candler, Mar 6, 2011
    #2
    1. Advertising

  3. On Mar 6, 4:36 pm, Brian Candler <> wrote:
    > RichardOnRails wrote in post #985685:
    >
    > > I haven't been able to get rid of those blank lines.  Is there a way?

    >
    > It looks like erb comments don't support the -%> way of preventing the
    > blank lines. I tried both erb and eruby command-line tools.
    >
    > Regular ruby comments seem OK though:
    >
    > <% # Example from -%>
    > <% #http://www.linuxtopia.org/online_books/programming... -%>
    > This text is <% a = 100; print "#{a}% Live!" %>
    >
    > BTW it's bad practice to embed print inside erb (and you'll see the
    > 'erb' command line tool gives unexpected results, maybe other erb
    > implementations will too). This is because you may find erb being
    > assembled into a buffer and then printed at the end.
    >
    > Much better to use <%= ... %> to embed output. For example:
    >
    > This text is <% a = 100 %><%= "#{a}% Live!" %>
    >
    > or:
    >
    > This text is <%= a = 100; "#{a}% Live!" %>
    >
    > --
    > Posted viahttp://www.ruby-forum.com/.


    Hi Brian,

    > I tried both erb and eruby command-line tools

    I didn't know there was an erb command-line executable. Thanks for
    that tidbit.

    > BTW it's bad practice to embed print inside erb

    Yeah, I thought that was a somewhat weird approach, but I wanted to
    get some of the published examples working before I began sticking in
    my 2-cents worth, aside from comments to compensate for my poor
    memory :)

    I had completely run out of ideas. Thanks for experimenting with the
    "<% #" idea. I wound up with the following code (published at
    http://www.pastie.org/1641092), which works perfectly IMHO:

    === Start 9 lines (no newline at end of 9th line) ====
    <% # Test_eruby_01.erb %>
    <% # K:\_Projects\Ruby\_Ruby_Tests\TestEruby %>
    <% # Example from %>
    <% # http://www.linuxtopia.org/online_bo...by_tutorial/Ruby_and_the_Web_Using_eruby.html
    %>
    <% # Usage: %>
    <% # eruby Test_eruby_01.erb %>
    <% # Published: %>
    <% # http://www.pastie.org/1641092 %>
    This text is <% a = 100; print "#{a}% Live!" %>
    === End 9 lines (4th line is split in this posting) ===

    Output in Command Window (one line):
    === Start ===
    K:\_Projects\Ruby\_Ruby_Tests\TestEruby>eruby Test_eruby_01.erb
    This text is 100% Live!
    K:\_Projects\Ruby\_Ruby_Tests\TestEruby>

    Best wishes,
    Richard
     
    RichardOnRails, Mar 6, 2011
    #3
  4. On Sun, Mar 6, 2011 at 1:55 AM, RichardOnRails
    <> wrote:
    > Hi,
    >
    > I'm running:
    > Ruby 1.8.6
    > eRuby 1.0.5
    > WinXP-Pro/SP3
    >
    > I've got a three-line script:
    > <%#- Example from -%>
    > <%#- http://www.linuxtopia.org/online_books/programming_books/ruby_tutori=

    al/Ruby_and_the_Web_Using_eruby.html
    > -%>
    > This text is <% a =3D 100; print "#{a}% Live!" %>
    > =3D=3D=3D end of script =3D=3D=3D
    >
    > that produces 5 lines of output in a Command Window (2 blank lines, a
    > line of text and 2 more blank lines):
    > K:\_Projects\Ruby\_Ruby_Tests\TestEruby>eruby Test_eruby_01.erb
    >
    >
    > This text is 100% Live!
    >
    >
    > K:\_Projects\Ruby\_Ruby_Tests\TestEruby>
    > =3D=3D=3D end of relevant portion of Cmd Window =3D=3D=3D
    >
    > I haven't been able to get rid of those blank lines. =A0Is there a way?


    Use less tags and let them span multiple lines. Remember that erb will
    copy everything literally that is not between <%%> so line endings are
    simply copied over. You can do

    13:28:16 Temp$ erb19 x.erb
    This text is 100% Live!"
    13:29:44 Temp$ cat -n x.erb
    1 <%#-
    2 Example from
    3 http://www.linuxtopia.org/online_books/programming_books/ruby_tut=
    orial/Ruby_and_the_Web_Using_eruby.html
    4 -%>This text is <% a =3D 100 %><%=3D a %>% Live!"
    13:29:45 Temp$

    And, btw, note to not use print, puts and the line in erb templates.
    If you want to include output in the file you need to use <%=3D %>
    because print may send output to a completely different place plus
    even if you catch the same (i.e. stdout) order may be wrong.

    Kind regards

    robert

    --=20
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
     
    Robert Klemme, Mar 7, 2011
    #4
    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. Chris Smith
    Replies:
    2
    Views:
    4,190
    Chris Smith
    Jul 4, 2003
  2. Steve V
    Replies:
    6
    Views:
    249
    Steve V
    Apr 20, 2005
  3. Wes Gamble
    Replies:
    2
    Views:
    85
    Wes Gamble
    Jan 18, 2009
  4. Dan Thomas

    link to .erb file, from .erb file

    Dan Thomas, Jan 26, 2011, in forum: Ruby
    Replies:
    4
    Views:
    176
    Dan Thomas
    Jan 26, 2011
  5. Cah Sableng
    Replies:
    0
    Views:
    247
    Cah Sableng
    Apr 23, 2007
Loading...

Share This Page