Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixes #15732 -- better introduction of template filter decorator conc…

…epts (first register, then stringfilter). Thanks for the patch, hahasee.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@16251 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 803a454aefaca780d0614a8053f3bfe8a76e6876 1 parent 90e8e48
Chris Beaven authored May 20, 2011

Showing 1 changed file with 21 additions and 19 deletions. Show diff stats Hide diff stats

  1. 40  docs/howto/custom-template-tags.txt
40  docs/howto/custom-template-tags.txt
@@ -105,23 +105,6 @@ your function. Example::
105 105
         "Converts a string into all lowercase"
106 106
         return value.lower()
107 107
 
108  
-Template filters that expect strings
109  
-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
110  
-
111  
-If you're writing a template filter that only expects a string as the first
112  
-argument, you should use the decorator ``stringfilter``. This will
113  
-convert an object to its string value before being passed to your function::
114  
-
115  
-    from django.template.defaultfilters import stringfilter
116  
-
117  
-    @stringfilter
118  
-    def lower(value):
119  
-        return value.lower()
120  
-
121  
-This way, you'll be able to pass, say, an integer to this filter, and it
122  
-won't cause an ``AttributeError`` (because integers don't have ``lower()``
123  
-methods).
124  
-
125 108
 Registering custom filters
126 109
 ~~~~~~~~~~~~~~~~~~~~~~~~~~
127 110
 
@@ -140,18 +123,37 @@ The ``Library.filter()`` method takes two arguments:
140 123
 You can use ``register.filter()`` as a decorator instead::
141 124
 
142 125
     @register.filter(name='cut')
143  
-    @stringfilter
144 126
     def cut(value, arg):
145 127
         return value.replace(arg, '')
146 128
 
147 129
     @register.filter
148  
-    @stringfilter
149 130
     def lower(value):
150 131
         return value.lower()
151 132
 
152 133
 If you leave off the ``name`` argument, as in the second example above, Django
153 134
 will use the function's name as the filter name.
154 135
 
  136
+Template filters that expect strings
  137
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  138
+
  139
+If you're writing a template filter that only expects a string as the first
  140
+argument, you should use the decorator ``stringfilter``. This will
  141
+convert an object to its string value before being passed to your function::
  142
+
  143
+    from django import template
  144
+    from django.template.defaultfilters import stringfilter
  145
+
  146
+    register = template.Library()
  147
+
  148
+    @register.filter
  149
+    @stringfilter
  150
+    def lower(value):
  151
+        return value.lower()
  152
+
  153
+This way, you'll be able to pass, say, an integer to this filter, and it
  154
+won't cause an ``AttributeError`` (because integers don't have ``lower()``
  155
+methods).
  156
+
155 157
 Filters and auto-escaping
156 158
 ~~~~~~~~~~~~~~~~~~~~~~~~~
157 159
 

0 notes on commit 803a454

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