attributes - Django-Components" > attributes - Django-Components" >
Skip to content

attributes ¤

append_attributes ¤

append_attributes(*args: Tuple[str, Any]) -> Dict

Merges the key-value pairs and returns a new dictionary.

If a key is present multiple times, its values are concatenated with a space character as separator in the final dictionary.

Source code in src/django_components/attributes.py
def append_attributes(*args: Tuple[str, Any]) -> Dict:
    """
    Merges the key-value pairs and returns a new dictionary.

    If a key is present multiple times, its values are concatenated with a space
    character as separator in the final dictionary.
    """
    result: Dict = {}

    for key, value in args:
        if key in result:
            result[key] += " " + value
        else:
            result[key] = value

    return result

attributes_to_string ¤

attributes_to_string(attributes: Mapping[str, Any]) -> str

Convert a dict of attributes to a string.

Source code in src/django_components/attributes.py
def attributes_to_string(attributes: Mapping[str, Any]) -> str:
    """Convert a dict of attributes to a string."""
    attr_list = []

    for key, value in attributes.items():
        if value is None or value is False:
            continue
        if value is True:
            attr_list.append(conditional_escape(key))
        else:
            attr_list.append(format_html('{}="{}"', key, value))

    return mark_safe(SafeString(" ").join(attr_list))