Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Template filters now pass numerical arguments through as numbers.

This was the (undocumented) behaviour prior to r10118 and now it's back
again. It's neither hard nor harmful to maintain compatibility with the
old ways.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@10169 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 2c6c60cec613246bff5cb0be831969c20417b53c 1 parent 7f63d00
Malcolm Tredinnick authored March 25, 2009
12  django/template/__init__.py
@@ -445,16 +445,17 @@ def value(self):
445 445
             self.pointer = i
446 446
             return s
447 447
 
  448
+# This only matches constant *strings* (things in quotes or marked for
  449
+# translation). Numbers are treated as variables for implementation reasons
  450
+# (so that they retain their type when passed to filters).
448 451
 constant_string = r"""
449 452
 (?:%(i18n_open)s%(strdq)s%(i18n_close)s|
450 453
 %(i18n_open)s%(strsq)s%(i18n_close)s|
451 454
 %(strdq)s|
452  
-%(strsq)s)|
453  
-%(num)s
  455
+%(strsq)s)
454 456
 """ % {
455 457
     'strdq': r'"[^"\\]*(?:\\.[^"\\]*)*"', # double-quoted string
456 458
     'strsq': r"'[^'\\]*(?:\\.[^'\\]*)*'", # single-quoted string
457  
-    'num': r'[-+\.]?\d[\d\.e]*', # numeric constant
458 459
     'i18n_open' : re.escape("_("),
459 460
     'i18n_close' : re.escape(")"),
460 461
     }
@@ -462,17 +463,18 @@ def value(self):
462 463
 
463 464
 filter_raw_string = r"""
464 465
 ^(?P<constant>%(constant)s)|
465  
-^(?P<var>[%(var_chars)s]+)|
  466
+^(?P<var>[%(var_chars)s]+|%(num)s)|
466 467
  (?:%(filter_sep)s
467 468
      (?P<filter_name>\w+)
468 469
          (?:%(arg_sep)s
469 470
              (?:
470 471
               (?P<constant_arg>%(constant)s)|
471  
-              (?P<var_arg>[%(var_chars)s]+)
  472
+              (?P<var_arg>[%(var_chars)s]+|%(num)s)
472 473
              )
473 474
          )?
474 475
  )""" % {
475 476
     'constant': constant_string,
  477
+    'num': r'[-+\.]?\d[\d\.e]*',
476 478
     'var_chars': "\w\." ,
477 479
     'filter_sep': re.escape(FILTER_SEPARATOR),
478 480
     'arg_sep': re.escape(FILTER_ARGUMENT_SEPARATOR),
11  tests/regressiontests/templates/custom.py
... ...
@@ -0,0 +1,11 @@
  1
+from django import test
  2
+from django import template
  3
+
  4
+
  5
+custom_filters = """
  6
+>>> t = template.Template("{% load custom %}{{ string|trim:5 }}")
  7
+>>> ctxt = template.Context({"string": "abcdefghijklmnopqrstuvwxyz"})
  8
+>>> t.render(ctxt)
  9
+u"abcde"
  10
+"""
  11
+
0  tests/regressiontests/templates/templatetags/__init__.py
No changes.
11  tests/regressiontests/templates/templatetags/custom.py
... ...
@@ -0,0 +1,11 @@
  1
+from django import template
  2
+from django.template.defaultfilters import stringfilter
  3
+
  4
+register = template.Library()
  5
+
  6
+def trim(value, num):
  7
+    return value[:num]
  8
+trim = stringfilter(trim)
  9
+
  10
+register.filter(trim)
  11
+
4  tests/regressiontests/templates/tests.py
@@ -18,9 +18,10 @@
18 18
 from django.utils.safestring import mark_safe
19 19
 from django.utils.tzinfo import LocalTimezone
20 20
 
21  
-from unicode import unicode_tests
22 21
 from context import context_tests
  22
+from custom import custom_filters
23 23
 from parser import filter_parsing, variable_parsing
  24
+from unicode import unicode_tests
24 25
 
25 26
 try:
26 27
     from loaders import *
@@ -34,6 +35,7 @@
34 35
     'unicode': unicode_tests,
35 36
     'context': context_tests,
36 37
     'filter_parsing': filter_parsing,
  38
+    'custom_filters': custom_filters,
37 39
 }
38 40
 
39 41
 #################################

0 notes on commit 2c6c60c

Please sign in to comment.
Something went wrong with that request. Please try again.