Ticket #4793: 4793.patch

File 4793.patch, 2.0 KB (added by Chris Beaven, 17 years ago)
  • docs/templates_python.txt

     
    642642        "Converts a string into all lowercase"
    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~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     647If you are writing a template filter which only expects a string as the first
     648argument, you should use the included decorator ``stringfilter`` which will convert
     649an object to it's string value before being passed to your function::
     650
     651    from django.template.defaultfilters import stringfilter
     652
     653    @stringfilter
     654    def lower(value):
     655        return value.lower()
     656
     657Registering custom template filters
     658~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     659
     660After you've written your filter definition, you need to register it with
    646661your ``Library`` instance, to make it available to Django's template language::
    647662
    648663    register.filter('cut', cut)
     
    658673decorator instead::
    659674
    660675    @register.filter(name='cut')
     676    @stringfilter
    661677    def cut(value, arg):
    662678        return value.replace(arg, '')
    663679
    664680    @register.filter
     681    @stringfilter
    665682    def lower(value):
    666683        return value.lower()
    667684
    668685If you leave off the ``name`` argument, as in the second example above, Django
    669686will use the function's name as the filter name.
    670687
    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::
    676688
    677     from django.template.defaultfilters import stringfilter
    678 
    679     @stringfilter
    680     def lower(value):
    681         return value.lower()
    682 
    683689Writing custom template tags
    684690----------------------------
    685691
Back to Top