Opened 19 years ago

Closed 18 years ago

#449 closed enhancement (wontfix)

[patch] variable selection for templates should allow negative list indexes

Reported by: hugo <gb@…> Owned by: Adrian Holovaty
Component: Template system Version:
Severity: normal Keywords:
Cc: paul.bowsher@… Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Currently you can access elements of a list by adding the numeric index - but you can only use positive indexes. This should be extended to negative indexes as well, so that you could last elements in lists.

Change History (3)

comment:1 by Boffbowsh, 19 years ago

Summary: variable selection for templates should allow negative list indexes[patch] variable selection for templates should allow negative list indexes
Index: /usr/local/django_src/django/core/template.py
===================================================================
--- /usr/local/django_src/django/core/template.py       (revision 593)
+++ /usr/local/django_src/django/core/template.py       (working copy)
@@ -71,7 +71,7 @@
 VARIABLE_TAG_START = '{{'
 VARIABLE_TAG_END = '}}'

-ALLOWED_VARIABLE_CHARS = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_.'
+ALLOWED_VARIABLE_CHARS = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_.-'

 # match a variable or block tag and capture the entire tag, including start/end delimiters
 tag_re = re.compile('(%s.*?%s|%s.*?%s)' % (re.escape(BLOCK_TAG_START), re.escape(BLOCK_TAG_END),

Test:

>>> from django.core import template
>>> t = '''{{ l.-1 }}'''
>>> l = [5, 2, 9, 4]
>>> >>> tem = template.Templat l = [5, 2, 9, 4]
KeyboardInterrupt
>>> tem = template.Template(t)
>>> c = template.Context({'l': l})
>>> tem.render(c)
'4'

comment:2 by anonymous, 19 years ago

Cc: paul.bowsher@… added

comment:3 by Adrian Holovaty, 18 years ago

Resolution: wontfix
Status: newclosed

I'm marking this as a wontfix because it's too much of an edge case.

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