Opened 15 months ago
Closed 14 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 , 15 months ago
| Triage Stage: | Unreviewed → Accepted |
|---|
comment:2 by , 15 months ago
| Owner: | set to |
|---|---|
| Status: | new → assigned |
comment:3 by , 15 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 , 15 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:6 by , 15 months ago
| Patch needs improvement: | set |
|---|
comment:7 by , 15 months ago
| Patch needs improvement: | unset |
|---|---|
| Triage Stage: | Accepted → Ready for checkin |
Makes totally sense to me!