Code

Opened 8 years ago

Closed 8 years ago

Last modified 8 years ago

#911 closed defect (fixed)

[patch] Make template system scoped to the parser

Reported by: rjwittams Owned by: adrian
Component: contrib.admin Version:
Severity: normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description (last modified by rjwittams)

So currently, template tags and filters are loaded into module level dictionaries.
This means that tags and filters are available in places that they haven't been loaded.

What this leads to is namespace pollution by previous requests - so your code might work one minute, as a previous request loaded the lib, and break the next if you are the first request.

So this patch moves filters and tags onto parsers. As a bonus, filters are now parsed at compile time rather than runtime, so they don't get parsed multiple times in loops.

The changes are:
At the top of a tag/filter library,
do

import django.core.template 

register = template.Library()

then to register tags or filters, use

register.tag('name', func)
register.filter('name', func) # No has_arg, see below

In tags, resolve_variable_with_filters is gone.
The functionality is split in two.
In your compile function, do

filter_expr =  parser.compile(filter_string)

pass this into your node class as usual.

in render, do

filter_result = filter_expr.resolve(context) 

instead of resolve_variable_with_filters.

the tag decorators are also now members of the library class, so do

@register.simple_tag

and

@register.inclusion_tag('cow_detail')

for those.

Filters can now have default arguments, and whether they have an argument is inferred from the signature. So any

def filter_func(obj, _):

should be changed to

def filter_func(obj):

and the has_arg argument is no longer accepted or useful.
Filter arguments can be also now be variables which are resolved in the context as well as constant strings.

Attachments (1)

django-template-scoping.diff (90.7 KB) - added by rjwittams 8 years ago.

Download all attachments as: .zip

Change History (6)

comment:1 Changed 8 years ago by adrian

  • Description modified (diff)

(Fixed formatting in description.)

comment:2 Changed 8 years ago by rjwittams

  • Description modified (diff)

Changed 8 years ago by rjwittams

comment:3 Changed 8 years ago by rjwittams

Changed documentation.

Also, the {% load %} tag can load multiple libraries in one go now:

{% load i18 admin_list adminmedia %}

And I added some defaults to filters where it seemed sane ( date formatting, yesno).

comment:4 Changed 8 years ago by rjwittams

  • Description modified (diff)

comment:5 Changed 8 years ago by adrian

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

(In [1443]) Fixed #911 -- Made template system scoped to the parser instead of the template module. Also changed the way tags/filters are registered and added support for multiple arguments to {% load %} tag. Thanks, rjwittams. This is a backwards-incompatible change for people who've created custom template tags or filters. See http://code.djangoproject.com/wiki/BackwardsIncompatibleChanges for upgrade instructions.

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.