Reputation: 95
I'm trying to do an inline if
with Ruby Slim.
Given my example below...
- if @droppable
.panel data-draggable="true"
span More content here
- else
.panel
span More content here
In both cases, the only difference is the presence of the data-draggable
attribute on the top-level .panel
element. The contents of the .panel
is identical, so I'd like to accomplish something like the following:
.panel (@droppable ? data-draggable="true")
span More content here
Is there a way of achieving this in Slim?
Upvotes: 2
Views: 4029
Reputation: 403
You can't have an "inline if" , but you can get the behavior you want with slim in line html support doing this:
<div class="cool_class"
- if conditional_is_met
| data="must_be_added"
| special_data="#{@my_data.to_json}"
|
</div>
consider that inside the html tag the slim identation is still followed.
and the final |
is important to close the if.
Upvotes: 0
Reputation: 239382
There is no need for an if
here, and the ternary operator requires three operands, not two.
Both Slim and HAML are designed to omit attributes with nil/false values, intentionally letting you use the &&
operator to turn a truthy/falsy value to the presence of attribute with a specific value, or its absence:
In Slim:
.panel data-draggable=(@droppable && "true")
span Hello
In HAML:
.panel{data: {draggable: @droppable && "true"}}
%span Hello
In both cases, if @droppable
is a truthy value, data-draggable="true"
will be added, otherwise the attribute will be omitted.
Upvotes: 2
Reputation: 121000
Use dynamic tags:
ruby:
def panel!
{tag: 'panel'}.tap do |tag|
tag[:"data-draggable"] = true if @droppable
end
end
*panel!
span hello
Upvotes: 0