Piioo Piioo - 22 days ago 4
Ruby Question

Remove HTML tag attribute in slim when attribute should not be displayed

I would like to remove class attribute when class should not be displayed in Slim.

In ERB, I could use:

<input <%= "class='foo'" if false %> />
<input />

How do I do this in Slim?

I found this, but I feel there must be a more idiomatic solution:

| <input "#{'class=\"foo\"' if false}" />


If the value of an attribute is nil, then the entire attribute will be omitted (actually this is happens for nil or false for most cases, but it looks like the class attribute behaves differently for false and true):

input class=('foo' if condition)

This will output

<input class="foo" />

if condition is true, and

<input />

if condition is false.