Opened 3 months ago

Closed 3 months ago

#35668 closed New feature (fixed)

Expand format_html_join() to allow keyword formatting

Reported by: Adam Johnson Owned by: Mohamed Nabil Rady
Component: Utilities Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

`format_html_join()` currently allows formatting positional arguments only. It would be nice if it expanded support to allow keyword arguments, like the underlying format_html() does. Keyword arguments would come from an iterable of mappings (dicts and dict-like objects). This would allow less repetition when a value is used multiple times in the template string, like:

from django.utils.html import format_html_join

html = format_html_join(
    "\n",
    '<li data-id="{id}">{title} ({id})</li>',
    [
        {"id": book.id, "title": book.title}
        for book in books
    ]
)

and even:

from django.utils.html import format_html_join

html = format_html_join(
    "\n",
    '<li data-id="{id}">{title} ({id})</li>',
    Book.objects.values("id", "title")
)

Change History (8)

comment:1 by Claude Paroz, 3 months ago

Triage Stage: UnreviewedAccepted

Makes totally sense to me!

comment:2 by Mohamed Nabil Rady, 3 months ago

Owner: set to Mohamed Nabil Rady
Status: newassigned

comment:3 by Mohamed Nabil Rady, 3 months ago

How should the function behave if we want to use only args, only kwargs, or both ?

from django.utils.html import format_html_join

html = format_html_join(
    "\n",
    '<li data-id="{id}">{title} ({id})</li>',
    [
        {"id": book.id, "title": book.title}
        for book in books
    ]
)

How will this example work ? The third argument should the args generator.

comment:4 by Adam Johnson, 3 months ago

The idea is that if the third argument contains mappings (dicts or dict-like objects), the formatting uses them as kwaegs, rather than as args. Arg and kwarg modes will be exclusive.

comment:5 by Mohamed Nabil Rady, 3 months ago

Has patch: set

comment:6 by Adam Johnson, 3 months ago

Patch needs improvement: set

comment:7 by Adam Johnson, 3 months ago

Patch needs improvement: unset
Triage Stage: AcceptedReady for checkin

comment:8 by Sarah Boyce <42296566+sarahboyce@…>, 3 months ago

Resolution: fixed
Status: assignedclosed

In 231c0d85:

Fixed #35668 -- Added mapping support to format_html_join.

Note: See TracTickets for help on using tickets.
Back to Top