Convert a dictionary of attributes to a single string. The returned string will contain a leading space followed by key="value", XML-style pairs. In the case of a boolean value, the key will appear without a value. It is assumed that the keys do not need to be XML-escaped. If th
(attrs)
| 18 | |
| 19 | |
| 20 | def flatatt(attrs): |
| 21 | """ |
| 22 | Convert a dictionary of attributes to a single string. |
| 23 | The returned string will contain a leading space followed by key="value", |
| 24 | XML-style pairs. In the case of a boolean value, the key will appear |
| 25 | without a value. It is assumed that the keys do not need to be |
| 26 | XML-escaped. If the passed dictionary is empty, then return an empty |
| 27 | string. |
| 28 | |
| 29 | The result is passed through 'mark_safe' (by way of 'format_html_join'). |
| 30 | """ |
| 31 | key_value_attrs = [] |
| 32 | boolean_attrs = [] |
| 33 | for attr, value in attrs.items(): |
| 34 | if isinstance(value, bool): |
| 35 | if value: |
| 36 | boolean_attrs.append((attr,)) |
| 37 | elif value is not None: |
| 38 | key_value_attrs.append((attr, value)) |
| 39 | |
| 40 | return format_html_join("", ' {}="{}"', sorted(key_value_attrs)) + format_html_join( |
| 41 | "", " {}", sorted(boolean_attrs) |
| 42 | ) |
| 43 | |
| 44 | |
| 45 | class RenderableMixin: |