Forum: Ruby on Rails <% if ... -%> What is this?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Mark H. (Guest)
on 2006-01-04 17:55
I seen some RoR code that went like this:

<% if ...blah, blah...  -%>

.. blah, blah

<% end -%>

What are the minus signs for in the if statement?  Cannot seem to find
anything about this, and searching on "-" in this context is hard to get
anything meaningfull back.


Thanks,

- Mark
Lee Pope (Guest)
on 2006-01-04 19:13
(Received via mailing list)
I asked this question a while back, so might as well take this one.

It's undocumented, you can get it if you look at the erb code.

It means 'evaluate this code, but don't insert a newline afterwards'

Saves you from having a bunch of whitespace in your output.
Bruce B. (Guest)
on 2006-01-04 19:13
(Received via mailing list)
The minus sign means that the code will take no space on the page if
the IF does not fire.  I think they call it "supressing the newline
that follows the %> delimiter.

bruce
Mark H. (Guest)
on 2006-01-04 20:15
Lee Pope wrote:
> I asked this question a while back, so might as well take this one.
>
> It's undocumented, you can get it if you look at the erb code.
>
> It means 'evaluate this code, but don't insert a newline afterwards'
>
> Saves you from having a bunch of whitespace in your output.

Ok, this makes sense.

Thanks guys.
sean lynch (Guest)
on 2006-01-04 21:33
The "-%>" causes ERb to suppress the newline on any output. If you have
output, it will not have an ending newline, if you have no output, you
will not get meaningless blank lines.

It only seems to be documented in the ERb.rb source code:
http://raa.ruby-lang.org/gonzui/markup/ruby/lib/erb.rb

( Do a find on :ExplicitTrimRegexp )

The ERb documentation says you can suppress newlines with the trim_mode
setting, but does not say what the default trim_mode character is.

-Sean

Mark H. wrote:
> I seen some RoR code that went like this:
>
> <% if ...blah, blah...  -%>
>
> .. blah, blah
>
> <% end -%>
>
> What are the minus signs for in the if statement?  Cannot seem to find
> anything about this, and searching on "-" in this context is hard to get
> anything meaningfull back.
>
>
> Thanks,
>
> - Mark
Stefan K. (Guest)
on 2006-01-04 22:24
(Received via mailing list)
sean lynch wrote:

>setting, but does not say what the default trim_mode character is.
>>.. blah, blah
>>- Mark
>>
>>
See also
http://railsexpress.de/blog/articles/2005/12/06/tr...

--
For rails performance tuning, see: http://railsexpress.de/blog
Subscription: http://railsexpress.de/blog/xml/rss20/feed.xml
Steven S. (Guest)
on 2006-01-05 04:24
(Received via mailing list)
This notation is typically used on embedded Ruby (ERb) lines that
produce no output (if, end, etc.) so that no extraneous blank lines
are produced in the HTML.
TJ Stankus (Guest)
on 2006-01-05 11:08
(Received via mailing list)
> <% if ...blah, blah...  -%>

> What are the minus signs for in the if statement?  Cannot seem to find

The answer is already in this thread, but just FYI this is covered in
the Agile Web D. with Rails book.

-TJ
This topic is locked and can not be replied to.