Opened 5 years ago

Last modified 5 years ago

#30205 new New feature

New built-in tag to disable invoking callable variables during template variable resolution

Reported by: Alex Epshteyn Owned by: nobody
Component: Template system Version: dev
Severity: Normal Keywords: template, template variable, variable resolution, template tag
Cc: Triage Stage: Someday/Maybe
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

To improve upon the sub-optimal workaround introduced by #15791, I propose the following non-breaking change:

Proposal for new built-in tag callables [on|off]

Introduce a new built-in tag similar to autoescape, that would allow disabling the implicit invocation of callable variables in a particular template block. For example:

{% callables off %}
  <div>The class name is {{ foo.bar|type_name }}</div>
{% endcallables %}

Any variable resolutions performed inside this block would not implicitly invoke a callable during variable resolution unless the previous "bit" in the "lookups" path for this variable was an instance and the current "bit" is a method defined for the instance (see django.template.base.Variable._resolve_lookup).

My justification for this feature request is detailed in #30197 (specifically, this comment).

The introduction of this new callables [on|off] tag would be a non-breaking way to:

  1. solve the frequently-reported problems described in #30197, #15791, #29382, and #29306
  2. adhere to the fundamental Django design philosophies of Loose coupling (by moving template-specific concerns from the application layer to the template layer), Less code, and Explicit is better than implicit.

Change History (5)

comment:1 by Tim Graham, 5 years ago

Triage Stage: UnreviewedSomeday/Maybe

As with #30197, please make your proposal on the DevelopersMailingList. I can't remember the last time a new template tag was added. I'm skeptical since "the template system is meant to express presentation, not program logic". Something like {% callables off %} doesn't seem aligned with that philosophy.

comment:2 by Carlton Gibson, 5 years ago

...sub-optimal workaround...

You're always welcome to add such a tag in your own project, but I'm not convinced that do_not_call_in_templates is sub-optimal at all. It's simple, explicit, effective, etc. Having two ways to do things isn't necessary.

I'm inclined to close this as wontfix unless there is a consensus to change it. (Rather than having it open forever as a Someday/Maybe.)

comment:3 by Tim Graham, 5 years ago

I bumped to someday/maybe merely so as not to appear overly aggressive following #30197 also closed as wontfix. If there's no follow up on django-developers within a week or two, I would close this ticket as wontfix rather than keep it open forever.

I haven't look into implementing the proposed tag, but my gut says it may not be possible to implement as a third-party tag without some hook in Django (i.e. how could a tag change the behavior of Variable._resolve_lookup()?)

comment:4 by Carlton Gibson, 5 years ago

There’s always a way… But fine, OK, let’s leave it for now. As ever, the DevelopersMailingList is the place to go. Thanks Tim.

in reply to:  1 comment:5 by Alex Epshteyn, 5 years ago

Replying to Tim Graham:

As with #30197, please make your proposal on the DevelopersMailingList. I can't remember the last time a new template tag was added. I'm skeptical since "the template system is meant to express presentation, not program logic". Something like {% callables off %} doesn't seem aligned with that philosophy.

That sounds reasonable. I just posted a new topic about this on the django-developers mailing list. Might need approval from a moderator on the Google Group for the post to go through.

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