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.
F28f75d17a0c236e079c6ebbb8fff2b9?d=identicon&s=25 Mark Haliday (markh)
on 2006-01-04 16: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
B79f6edafee26860eb2ef239558bf8dc?d=identicon&s=25 Lee Pope (Guest)
on 2006-01-04 18: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.
2dd904ec5981c31e7bb7a5743a53caf8?d=identicon&s=25 Bruce Balmer (brucebalmer)
on 2006-01-04 18: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
F28f75d17a0c236e079c6ebbb8fff2b9?d=identicon&s=25 Mark Haliday (markh)
on 2006-01-04 19: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.
8716cce9a7b721786e5425b8cc1f9951?d=identicon&s=25 sean lynch (Guest)
on 2006-01-04 20: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 Haliday 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
E555e7c34196967444a47a96395a23ab?d=identicon&s=25 Stefan Kaes (Guest)
on 2006-01-04 21: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
4353600bbb6b6e0950e62f7f44e49dd5?d=identicon&s=25 Steven Smith (Guest)
on 2006-01-05 03: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.
C693bf945a77e556113eee5003726343?d=identicon&s=25 TJ Stankus (Guest)
on 2006-01-05 10: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 Development with Rails book.

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