Django

Code

Changeset 6143

Show
Ignore:
Timestamp:
09/13/07 18:30:26 (1 year ago)
Author:
mtredinnick
Message:

Fixed #4793 -- Tweaked custom filter documentation a little to possibly reduce some confusion. Thanks, SmileyChris?.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/docs/templates_python.txt

    r5927 r6143  
    643643        return value.lower() 
    644644 
    645 When you've written your filter definition, you need to register it with 
     645Template filters which expect strings 
     646~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     647 
     648If you're writing a template filter which only expects a string as the first 
     649argument, you should use the included decorator ``stringfilter``. This will 
     650convert 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 
     658Registering a custom filters 
     659~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     660 
     661Once you've written your filter definition, you need to register it with 
    646662your ``Library`` instance, to make it available to Django's template language:: 
    647663 
     
    659675 
    660676    @register.filter(name='cut') 
     677    @stringfilter 
    661678    def cut(value, arg): 
    662679        return value.replace(arg, '') 
    663680 
    664681    @register.filter 
    665     def lower(value): 
    666         return value.lower() 
    667  
    668 If you leave off the ``name`` argument, as in the second example above, Django 
    669 will use the function's name as the filter name. 
    670  
    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  
    679682    @stringfilter 
    680683    def lower(value): 
    681684        return value.lower() 
     685 
     686If you leave off the ``name`` argument, as in the second example above, Django 
     687will use the function's name as the filter name. 
    682688 
    683689Writing custom template tags