<% if ... -%> What is this?


#1

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

#2

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.


#3

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


#4

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.


#5

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

#6

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.


#7

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/trim-your-output


For rails performance tuning, see: http://railsexpress.de/blog
Subscription: http://railsexpress.de/blog/xml/rss20/feed.xml


#8

<% 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