if statements with ternary operators

765 views
Skip to first unread message

Brewster

unread,
Mar 31, 2010, 8:39:21 PM3/31/10
to Haml
i have been struggling with using an if statement in my haml markup.

in my layout i have:

-if controller.action_name == 'index'
= controller.action_name.titleize

however, i wanted to implement that inline in my title

%title= controller.action_name == 'index' ?
controller.action_name.titleize

the latter does not work, and i have tried a variety of different
syntaxes, but i am just not having any success. i am releatively new
to ruby, and a noob with haml, so hoepfully there is something obvious
i am overlooking.

thanks!

Nathan Weizenbaum

unread,
Mar 31, 2010, 8:56:40 PM3/31/10
to ha...@googlegroups.com
A ternary operator has three components (hence the name): it's of the form "condition ? value1 : value2", where value1 is used if condition is true, and value2 is used otherwise. In your example, you're missing value2.

If you only have one value, you can do this: "%title= value if condition".


--
You received this message because you are subscribed to the Google Groups "Haml" group.
To post to this group, send email to ha...@googlegroups.com.
To unsubscribe from this group, send email to haml+uns...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/haml?hl=en.


Brewster

unread,
Mar 31, 2010, 10:53:27 PM3/31/10
to Haml
right, i intended to include the 3rd value but it was a bit of a rush
post.
what would the proper syntax for "condition ? val1 : val2" be in haml?

> > haml+uns...@googlegroups.com <haml%2Bunsu...@googlegroups.com>.

Chris Eppstein

unread,
Mar 31, 2010, 11:37:09 PM3/31/10
to ha...@googlegroups.com
%title= condition ? val1 : val2

To unsubscribe from this group, send email to haml+uns...@googlegroups.com.

Brewster

unread,
Apr 1, 2010, 1:37:51 AM4/1/10
to Haml
hmm ok. i tried applying that format to append the existing text in
the title, with no success.

%title= 'Site Name :: ' + controller.action_name == 'index' ?
controller.action_name.titleize

> > haml%2Bunsu...@googlegroups.com<haml%252Buns...@googlegroups.com>

Alex Wallace

unread,
Apr 1, 2010, 11:11:17 AM4/1/10
to ha...@googlegroups.com
That ternary statement is incorrect formed. You'll also need to add parentheses around the statement; otherwise Haml will start by trying to concatenate true/false onto the title, resulting in a TypeError.

A correct version might be:

%title= 'Site Name :: ' + (action_name == "index" ? "Index" : "") 

The titleized form of "index" will always be "Index", so I'm not sure why that just isn't written explicitly... maybe you just want something like:

%title~ "Site Name :: #{action_name.titleize}"

Either way it doesn't make much sense to me, but you'll need parentheses and the third part of the ternary operator (the "else" clause)

Best,
Alex

To unsubscribe from this group, send email to haml+uns...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages