Django

Code

Changeset 3772

Show
Ignore:
Timestamp:
09/21/06 06:41:18 (2 years ago)
Author:
mtredinnick
Message:

Fixed #2556 -- Documented that simple_tag functions can take any number of
arguments, not just one.

Files:

Legend:

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

    r3717 r3772  
    764764~~~~~~~~~~~~~~~~~~~~~~~~ 
    765765 
    766 Many template tags take a single argument -- a string or a template variable 
    767 reference -- and return a string after doing some processing based solely on 
     766Many template tags take a number of arguments -- strings or a template variables 
     767-- and return a string after doing some processing based solely on 
    768768the input argument and some external information. For example, the 
    769769``current_time`` tag we wrote above is of this variety: we give it a format 
     
    772772To ease the creation of the types of tags, Django provides a helper function, 
    773773``simple_tag``. This function, which is a method of 
    774 ``django.template.Library``, takes a function that accepts one argument, wraps 
    775 it in a ``render`` function and the other necessary bits mentioned above and 
    776 registers it with the template system. 
     774``django.template.Library``, takes a function that accepts any number of 
     775arguments, wraps it in a ``render`` function and the other necessary bits 
     776mentioned above and registers it with the template system. 
    777777 
    778778Our earlier ``current_time`` function could thus be written like this:: 
     
    790790 
    791791A couple of things to note about the ``simple_tag`` helper function: 
    792     * Only the (single) argument is passed into our function. 
    793792    * Checking for the required number of arguments, etc, has already been 
    794793      done by the time our function is called, so we don't need to do that. 
    795794    * The quotes around the argument (if any) have already been stripped away, 
    796795      so we just receive a plain string. 
     796    * If the argument was a template variable, our function is passed the 
     797      current value of the variable, not the variable itself. 
     798 
     799When your template tag does not need access to the current context, writing a 
     800function to work with the input values and using the ``simple_tag`` helper is 
     801the easiest way to create a new tag. 
    797802 
    798803Inclusion tags