Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #4793 -- Tweaked custom filter documentation a little to possib…

…ly reduce some confusion. Thanks, SmileyChris.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@6143 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 1b0e58811841858a6108ea7188625d3bd0d41ace 1 parent ce249d4
Malcolm Tredinnick authored September 13, 2007

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

  1. 32  docs/templates_python.txt
32  docs/templates_python.txt
@@ -642,7 +642,23 @@ your function. Example::
642 642
         "Converts a string into all lowercase"
643 643
         return value.lower()
644 644
 
645  
-When you've written your filter definition, you need to register it with
  645
+Template filters which expect strings
  646
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  647
+
  648
+If you're writing a template filter which only expects a string as the first
  649
+argument, you should use the included decorator ``stringfilter``. This will
  650
+convert an object to it's string value before being passed to your function::
  651
+
  652
+    from django.template.defaultfilters import stringfilter
  653
+
  654
+    @stringfilter
  655
+    def lower(value):
  656
+        return value.lower()
  657
+
  658
+Registering a custom filters
  659
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  660
+
  661
+Once you've written your filter definition, you need to register it with
646 662
 your ``Library`` instance, to make it available to Django's template language::
647 663
 
648 664
     register.filter('cut', cut)
@@ -658,28 +674,18 @@ If you're using Python 2.4 or above, you can use ``register.filter()`` as a
658 674
 decorator instead::
659 675
 
660 676
     @register.filter(name='cut')
  677
+    @stringfilter
661 678
     def cut(value, arg):
662 679
         return value.replace(arg, '')
663 680
 
664 681
     @register.filter
  682
+    @stringfilter
665 683
     def lower(value):
666 684
         return value.lower()
667 685
 
668 686
 If you leave off the ``name`` argument, as in the second example above, Django
669 687
 will use the function's name as the filter name.
670 688
 
671  
-Template filters which expect strings
672  
-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
673  
-If you are writing a template filter which only expects a string as the first
674  
-argument, you should use the included decorator ``stringfilter`` which will convert
675  
-an object to it's string value before being passed to your function::
676  
-
677  
-    from django.template.defaultfilters import stringfilter
678  
-
679  
-    @stringfilter
680  
-    def lower(value):
681  
-        return value.lower()
682  
-
683 689
 Writing custom template tags
684 690
 ----------------------------
685 691
 

0 notes on commit 1b0e588

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