Opened 7 years ago

Closed 4 years ago

Last modified 4 years ago

#6506 closed Bug (fixed)

simple include tag doesn't layer context like normal include tag

Reported by: kcarnold Owned by: nobody
Component: Template system Version: master
Severity: Normal Keywords: include custom tag library context layer inherit
Cc: kenneth.arnold@… Triage Stage: Design decision needed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

This was surprising behavior: unlike the provided include tag, user-defined include tags don't layer the provided context on top of the existing context.

It is consistent with a strict reading of the docs, but it's a confusing and un-motivated difference from ordinary include behavior.

I'm attaching a quick and dirty patch to fix this. The needed design decision is exactly how the context class should be handled; is there a use case for something other than just Context? (RequestContext needs another parameter...)

Attachments (1)

django-layer-context-in-include-tag.patch (857 bytes) - added by kcarnold 7 years ago.

Download all attachments as: .zip

Change History (7)

Changed 7 years ago by kcarnold

comment:1 Changed 7 years ago by mattmcc

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to invalid
  • Status changed from new to closed

Inclusion tags can receive the template context. See 'takes_context'. http://www.djangoproject.com/documentation/templates_python/#inclusion-tags

comment:2 Changed 7 years ago by kcarnold

  • Resolution invalid deleted
  • Status changed from closed to reopened

Point. (Yes I knew about that, but I didn't see the use case.)

Suggest adding to the docs something like: "You can replace

{% with id=thing.id %}{% include "other.html" %}{% endwith %}

with a custom tag:

@register.simple_tag('other.html', takes_context=True)
def other(context, id):
  context[id] = id
  return context

and simpler code:

{% other thing.id %}

Alternatively, make the previous proposed behavior the default if context_class is not provided, with the meaning "If I provide my own context class, I want to control how context handling happens. Otherwise, do it the way I'm used to include tags doing it.".

comment:3 Changed 7 years ago by jacob

  • Triage Stage changed from Unreviewed to Design decision needed

comment:4 Changed 7 years ago by kcarnold

  • Cc kenneth.arnold@… added

comment:5 Changed 4 years ago by julien

  • Resolution set to fixed
  • Severity set to Normal
  • Status changed from reopened to closed
  • Type set to Uncategorized

This has been fixed in #14987.

comment:6 Changed 4 years ago by SmileyChris

  • Type changed from Uncategorized to Bug
Note: See TracTickets for help on using tickets.
Back to Top