#34883 closed New feature (fixed)

Allow template tags to set extra data on templates.

Reported by: Carlton Gibson Owned by: Carlton Gibson
Component: Template system Version: 5.0
Severity: Normal Keywords:
Cc: Florian Apolloner 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

Custom template tags may need to pass additional data, beyond the node list, out of the parsing context, for later use, by (e.g.) the template loader, or other template clients.

Currently this is only feasible by attaching data to the origin object, since that's the only object available to both the template and the parser, but this isn't pretty.

I propose adding an additional attribute to the Parser that can be used by template tag authors to have extra data set on the template instance, that can then be used as needed.

This ticket comes from the forum discussion on Adding template fragments or partials for the DTL — it is the necessary minimal first change to allow an official[*] API such that a library such as django-template-partials can pass named node lists out of the parser for later use. (I'd imagine it would be useful to other tag library authors once in play.)

[*]: I say official, but I'm not sure whether to make this public — i.e. documented — it's pretty niche — those who need it'll find it. I'd be happy with a regression test making sure it didn't break but leaving it undocumented beyond that. Opinions welcome. 🙂

Change History (6)

comment:1 by Carlton Gibson, 15 months ago

comment:2 by Carlton Gibson, 15 months ago

Has patch: set

comment:3 by Carlton Gibson, 15 months ago

Owner: changed from nobody to Carlton Gibson

comment:4 by Mariusz Felisiak, 15 months ago

Triage Stage: UnreviewedAccepted

Seems reasonable.

comment:5 by Mariusz Felisiak, 15 months ago

Triage Stage: AcceptedReady for checkin

comment:6 by Mariusz Felisiak <felisiak.mariusz@…>, 15 months ago

Resolution: fixed
Status: assignedclosed

In 35bbb2c:

Fixed #34883 -- Allowed template tags to set extra data on templates.

By setting a value in the parser.extra_data mapping, template tags
pass additional data out of the parsing context.

Any extra data set is exposed on the template via the matching
.extra_data attribute.

Library authors should use a key to namespace extra data. The 'django'
namespace is reserved for internal use.

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