Expressing Conditional Haml Possibly with Ternary Operator

Expressing conditional HAML possibly with ternary operator

The code you have makes the text a child of the <br> element; that is not desirable. What you really meant, I think, was:

%br
- if @page.nil?
(nothing yet)
- else
#{@page.name}

For this you can simply do:

%br
#{@page.nil? ? "(nothing yet)" : @page.name}

Or

%br
= @page.nil? ? "(nothing yet)" : @page.name

Or simply:

<br>#{@page ? @page.name : "(nothing yet)"}

However, personally I would 'fix' this in the controller so that you always have a @page, with something like:

unless @page
@page = Page.new( name:"(nothing yet)", … )
end

With this you can stub out what a new/empty/nothing page looks like and let your view treat it like any other. Your @page still won't have a @page.id, so you can use that for tests to decide if you are creating a new item or editing an existing one.

This is how I handle all my forms that may be used to create or edit an item: provide defaults by creating (but not adding to the database) an item with the default values.

Aside: You're creating a <br> which is almost never a good idea, and you're creating it with Haml which should not be used for content markup. You might step back and think about what you're doing.

Ternary operator with haml and sprites

Ruby 1.9

%td
%i{class: list.active ? 'icon-ok' : 'icon-remove'}

Ruby 1.8

%td
%i{:class => list.active ? 'icon-ok' : 'icon-remove'}

Neat way to conditionally test whether to add a class in HAML template

You can inline the conditional to determine if the class is needed for the %li

%li{ :class => ('current' if section == "pages") }
%a{ :href => "#pages" } Pages

How to do an if/else in HAML without repeating indented code

!!!
- @user = "jed" #=> stubbing out an instance
%html
%head
- string = defined?(@user) ? "#{@user}" : nil #=> for demo only, wrap this in a helper method
%title{'data-account' => string}
%body
=yield

ternary operator based on if else

For something like this you might want to use a simple look-up table to eliminate some of the logic:

EQUIVALENT = {
'Y' => 'guest',
'N' => 'test'
}

if (a.present?)
b = EQUIVALENT[b.value] || b
end

The || b part may not be necessary if non-mapped b values are ignored.

Convert if else into ternary in ruby slim(temple engine)

Your first code can simply be replaced with this:

= value ? "yes" : "no"

Any lines starting with = are evaluated, and the resulting return value is inserted into the document after a call to escape_html.

Because you explained in your comments that you actually want HTML code to be inserted, you'll have to do this:

== value ? '<i class="fa fa-check-circle"></i>' : '<i class="fa fa-times-circle"></i>'

HAML class 2 if statements

You can use an array:

%li{class: [('active' if @current.empty?), ('disabled' if @past.empty?)]}

Any nil members of the array will be left out, and the remaining ones will be combined for the class attribute.

Why does a return statement break the conditional operator?

You can use it like this, by putting the entire return expression in parentheses:

def reflection
false ? true : (return false)
end

Of course, it does not make much sense used like this, but since you're experimenting (good!), the above works! The error is because of the way the Ruby grammar works I suppose - it expects a certain structure to form a valid expression.

UPDATE

Quoting some information from a draft specification:

An expression is a program construct which make up a statement (see 12
). A single expression can be a statement as an expression-statement
(see 12.2).12

NOTE A difference between an expression and a statement is that an
expression is ordinarily used where its value is required, but a
statement is ordinarily used where its value is not necessarily
required. However, there are some exceptions. For example, a
jump-expression (see 11.5.2.4) does not have a value, and the value
of the last statement of a compound-statement can be used.

NB. In the above, jump-expression includes return among others.

Using Polymer conditional attributes with HAML

One potential solution is to use the :plain filter to insert raw HTML into your HAML file:

:plain
<element attribute?={{boolean-expression}}></element>

A bit ugly, but it seems to work.

If you need to enclose some HAML-generated tags in one of these plain HTML tags, you'll need to use the :plain filter twice; once for the opening tag, and once for the closing tag.

:plain
<element attribute?={{boolean-expression}}>
-# HAML Content Here
:plain
</element>

Be sure not to indent your HAML code after the opening tag, otherwise it will become part of the "raw HTML" output and get sent as plain text to the browser instead of being processed as HAML.



Related Topics



Leave a reply



Submit