Haml 6 has
breaking changes
Show archive.org snapshot
regarding
boolean attributes
Show archive.org snapshot
. Only aria-
, data-
and a list of
select attributes
Show archive.org snapshot
(e.g. hidden
, disabled
) are considered "boolean".
"Boolean attributes" express meaning by their existence.
Show archive.org snapshot
. By this definition, Haml will, toggle boolean attributes by their value, and it will always assign a value to non-boolean attributes. E.g. %button{ disabled: !form.valid? }
will render as <button></button>
for a valid form, and as <button disabled></button>
for an invalid form. In contrast, a non-boolean attribute %div{ 'form-valid': form.valid? }
will render as either <div form-valid="true"></div>
or . Attributes without a value must be written as
%div(custom-boolean='')->
`.
Interpretation of "boolean" attributes does not change
Haml | HTML generated by Haml 5 & 6 |
---|---|
%button(disabled) | <button disabled></button> |
%button(disabled=true) | <button disabled></button> |
%button(disabled=false) | <button></button> |
%button(disabled='true') | <button disabled="true"></button> |
%button(disabled='false') | <button disabled="false"></button> |
%button(disabled='') | <button disabled=''></button> |
%button(data-test) | <button data-test></button> |
%button(data-test=true) | <button data-test></button> |
%button(data-test=false) | <button></button> |
%button(data-test='true') | <button data-test="true"></button> |
%button(data-test='false') | <button data-test="false"></button> |
%button(data-test='') | <button data-test=''></button> |
Other attributes will be handled differently by Haml 6
Haml | HTML generated by Haml 5 | HTML generated by Haml 6 |
---|---|---|
%button(up-test) | <button up-test></button> |
<button up-test="true"></button> |
%button(up-test=true) | <button up-test></button> |
<button up-test="true"></button> |
%button(up-test=false) | <button></button> |
<button up-test="false"></button> |
%button(up-test='true') | <button up-test="true"></button> |
same |
%button(up-test='false') | <button up-test="false"></button> |
same |
%button(up-test='') | <button up-test=''></button> |
same |
Extending the list of attributes considered "boolean" (monkey patch)
In case you want to reconfigure the attributes that a recognized as boolean attributes, you can use a monkey patch in an initializer. For example, the following would consider all attributes prefixed with app-
to be boolean attributes:
### Haml 6.3 and newer
Haml.class_eval do
remove_const(:BOOLEAN_ATTRIBUTES).tap do |boolean_attributes|
const_set(:BOOLEAN_ATTRIBUTES, [*boolean_attributes, /^app-/].freeze)
end
end
### Haml 6.0 - 6.2.x
Haml::AttributeBuilder.class_eval do
remove_const(:BOOLEAN_ATTRIBUTES).tap do |boolean_attributes|
const_set(:BOOLEAN_ATTRIBUTES, [*boolean_attributes, /^app-/].freeze)
end
end
Note: You might want to consider using data
attributes for all your custom attributes e.g. scrollable
=> data-scrollable
. It might still be legit to allow framework prefixes in case they do not support data
attributes e.g. ng-
(Angular) or up-
(Unpoly).
Boolean Unpoly Show archive.org snapshot attributes
Be very selective about patching up-
attributes so you can still use expressions like [up-follow=false]
or [up-history=false]
.
Starting with
Unpoly 3.8
Show archive.org snapshot
, most Unpoly attributes can now be enabled with a value "true"
and be disabled with a value "false"
. This pairs well with the default (unpatched) Haml 6 behavior.
A different approach is to create attributes from a ruby hash:
%tr{ { 'up-expand': ('' if superuser?) }.compact }
This will provide you with <tr up-expand="">
for a superuser and <tr>
otherwise.