Conditional replacements in rhtml


#1

On Wed, 25 Jan 2006 10:32:23 -0700, Ben R. removed_email_address@domain.invalid
wrote:

To do this with an if statement:

<%if @items.size == 1 -%>item<% else -%>items<% end -%>

Thanks you. Nonetheless I still remain uncertain why the following
construct does not work:

Your cart currently holds <%= @items.size %> <%= if @items.size == 1 then "item" else "items" %>.

If one considers only the issue of token parsing then surely the
construct <%= if @items.size == 1 ? “true” : “false” %> is
equivalent to <%= if @items.size == 1 then “true” else “false” %>.

It seems odd to me that the former works while the latter fails.
If there is a syntax reason why this behaviour is expected then I
would like to know what it is.

Regards,
Jim


*** e-mail is not a secure channel ***
mailto:byrnejb.@harte-lyne.ca
James B. Byrne Harte & Lyne Limited
vox: +1 905 561 1241 9 Brockley Drive
fax: +1 905 561 0757 Hamilton, Ontario
= hal Canada L8E 3C3

------- End of forwarded message -------

 *** e-mail is not a secure channel ***

mailto:byrnejb.@harte-lyne.ca
James B. Byrne Harte & Lyne Limited
vox: +1 905 561 1241 9 Brockley Drive
fax: +1 905 561 0757 Hamilton, Ontario
= hal Canada L8E 3C3


#2

James B. Byrne wrote:

If one considers only the issue of token parsing then surely the
construct <%= if @items.size == 1 ? “true” : “false” %> is
equivalent to <%= if @items.size == 1 then “true” else “false” %>.

It seems odd to me that the former works while the latter fails.
If there is a syntax reason why this behaviour is expected then I
would like to know what it is.

My guess would be, because Ruby needs to know where the line breaks are
in a multi-line if-then-else statement. If you put is all on one line
like:

if @items.size == 1 then “true” else “false”

You haven’t correctly told Ruby where the line breaks are located. You
might be able to fake it with some semi-colons, but this, I believe, is
why there is a one-line version of the if-then-else statement block…

-Brian


#3

If one considers only the issue of token parsing then surely the
construct <%= if @items.size == 1 ? “true” : “false” %> is
equivalent to <%= if @items.size == 1 then “true” else “false” %>.

I usually use the syntax <%=(@items.size == 1 ? “true” : “false”) %>

I’m not sure why this is, but in:

if @items.size == 1 ?

the “if” is not needed and may be just ignored with the presence of “?”.
That would explain why one would work while the other doesn’t. I’m just
trying to think from a parsing point of view and could be completely
wrong.


#4

I think what you’re after here is the pluralize helper.
http://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html#M000420

pluralize(@items.size, ‘item’) will result in “1 item”, or “2 items” etc

To use the syntax you described you would need to put an ‘end’ at the
end:

<%= if @items.size == 1 then “true” else “false” end %>

Cheers, -Jonny.

James B. Byrne wrote:

On Wed, 25 Jan 2006 10:32:23 -0700, Ben R. removed_email_address@domain.invalid
wrote:

To do this with an if statement:

<%if @items.size == 1 -%>item<% else -%>items<% end -%>

Thanks you. Nonetheless I still remain uncertain why the following
construct does not work:

Your cart currently holds <%= @items.size %> <%= if @items.size == 1 then "item" else "items" %>.

If one considers only the issue of token parsing then surely the
construct <%= if @items.size == 1 ? “true” : “false” %> is
equivalent to

It seems odd to me that the former works while the latter fails.
If there is a syntax reason why this behaviour is expected then I
would like to know what it is.

Regards,
Jim