Skip to content

Linter Rule: Disallow ERB silent tags in HTML attribute names ​

Rule: erb-no-silent-tag-in-attribute-name

Description ​

Disallow the use of ERB silent tags (<% ... %>) inside HTML attribute names. These tags introduce Ruby logic that does not output anything, and placing them in attribute names leads to malformed HTML or completely unpredictable rendering.

Rationale ​

HTML attribute names, ideally, must be valid, statically defined strings. Placing ERB silent tags (<% ... %>) that don't output anything inside attribute names might result in invalid HTML and is confusing. These cases are rarely intentional and almost always indicate a mistake or misuse of ERB templating.

Examples ​

✅ Good ​

erb
<div data-<%= key %>-target="value"></div>
Avoid ERB output in attribute names. Use static attribute names with dynamic values instead. (erb-no-output-in-attribute-name)
<div <%= data_attributes_for(user) %>></div>
Avoid `<%= %>` in attribute position. Use `<% if ... %>` with static attributes instead. (erb-no-output-in-attribute-position)

🚫 Bad ​

erb
<div data-<% key %>-id="value"></div>
Remove silent ERB tag from HTML attribute name. Silent ERB tags (`<%`) do not output content and should not be used in attribute names. (erb-no-silent-tag-in-attribute-name)
Avoid unused expressions in silent ERB tags. `key` is evaluated but its return value is discarded. Use `<%= ... %>` to output the value or remove the expression. (erb-no-unused-expressions)
<div data-<%# key %>-id="thing"></div>
Remove silent ERB tag from HTML attribute name. Silent ERB tags (`<%#`) do not output content and should not be used in attribute names. (erb-no-silent-tag-in-attribute-name)
<div data-<%- key -%>-id="thing"></div>
Remove silent ERB tag from HTML attribute name. Silent ERB tags (`<%-`) do not output content and should not be used in attribute names. (erb-no-silent-tag-in-attribute-name)
Avoid unused expressions in silent ERB tags. `key` is evaluated but its return value is discarded. Use `<%= ... %>` to output the value or remove the expression. (erb-no-unused-expressions)

References ​

-

Released under the MIT License.