Code

Opened 2 years ago

Closed 2 years ago

#17085 closed New feature (duplicate)

Deprecate "add_to_builtins" and replace with API on Template

Reported by: carljm Owned by: nobody
Component: Template system Version: master
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

add_to_builtins modifies global process state. This is to be avoided whenever possible, and in this case there would be a much better alternative: provide an API on a Template object to inject templatetag libraries as "built-in" for that Template. This provides the same effect as add_to_builtins but in a localized and explicit way. If the tag library should be effectively built-in for the whole project, just use a custom render_to_response equivalent (or TemplateResponse subclass) that uses this API to inject the library for each Template rendered.

Deprecating add_to_builtins is another step on the path of cleaning up globally-stored settings-related state in Django, which needs to be done if we're going to clean up settings as process-global.

Attachments (0)

Change History (4)

comment:1 Changed 2 years ago by carljm

  • Summary changed from Deprecate "add_to_builtins" to Deprecate "add_to_builtins" and replace with API on Template

comment:2 Changed 2 years ago by ptone

  • Triage Stage changed from Unreviewed to Accepted

add_to_builtins is not a documented function - but seems to have made it into use extensive in the wild.

its primary use currently in Django is either:

add the truly global builtin tags and filters in template/base.py

add template tags into builtin space as a convenience for tests

the API you propose sounds like a python level alternative to the load tag.

From the context of the template, this is magic and far less explicit than using the load tag

It looks like most of the wild use of add_to_builtins is to provide "convenience" by 3rd party apps to make their tags global. But I would say that they should be re-documented as requiring the load tag in the users base.html if they want it everywhere

I think a deprecation of add_to_builtins with a replacement of _add_to_builtins could be done to get the message. But I think we should be encouraging more explicitness in the template space, not enabling less.

comment:3 Changed 2 years ago by carljm

This ticket was based on the false premise that add_to_builtins was documented API - I really thought I'd seen it in a corner of the docs somewhere. Given that it's not, I think it should just be removed, and we don't need to add an API to replace it.

comment:4 Changed 2 years ago by carljm

  • Resolution set to duplicate
  • Status changed from new to closed

Closing this as dupe of #17093 since dealing with the add_to_builtins problem makes the most sense in the context of that refactor.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.