1
0
mirror of https://github.com/django/django.git synced 2024-12-24 18:16:19 +00:00

Fixed #622 -- Added default_if_none filter. Thanks, Eric

git-svn-id: http://code.djangoproject.com/svn/django/trunk@859 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Adrian Holovaty 2005-10-14 03:48:27 +00:00
parent f2088d456c
commit ed1c9b2046
2 changed files with 50 additions and 40 deletions

View File

@ -333,6 +333,12 @@ def default(value, arg):
"If value is unavailable, use given default" "If value is unavailable, use given default"
return value or arg return value or arg
def default_if_none(value, arg):
"If value is None, use given default"
if value is None:
return arg
return value
def divisibleby(value, arg): def divisibleby(value, arg):
"Returns true if the value is devisible by the argument" "Returns true if the value is devisible by the argument"
return int(value) % int(arg) == 0 return int(value) % int(arg) == 0

View File

@ -376,9 +376,9 @@ Built-in tag reference
========================== ================================================ ========================== ================================================
``forloop.counter`` The current iteration of the loop (1-indexed) ``forloop.counter`` The current iteration of the loop (1-indexed)
``forloop.counter0`` The current iteration of the loop (0-indexed) ``forloop.counter0`` The current iteration of the loop (0-indexed)
``forloop.revcounter`` The number of iterations from the end of the ``forloop.revcounter`` The number of iterations from the end of the
loop (1-indexed) loop (1-indexed)
``forloop.revcounter0`` The number of iterations from the end of the ``forloop.revcounter0`` The number of iterations from the end of the
loop (0-indexed) loop (0-indexed)
``forloop.first`` True if this is the first time through the loop ``forloop.first`` True if this is the first time through the loop
``forloop.last`` True if this is the last time through the loop ``forloop.last`` True if this is the last time through the loop
@ -569,25 +569,28 @@ Built-in filter reference
------------------------- -------------------------
``add`` ``add``
Adds the arg to the value Adds the arg to the value.
``addslashes`` ``addslashes``
Adds slashes - useful for passing strings to JavaScript, for example. Adds slashes. Useful for passing strings to JavaScript, for example.
``capfirst`` ``capfirst``
Capitalizes the first character of the value Capitalizes the first character of the value.
``center`` ``center``
Centers the value in a field of a given width Centers the value in a field of a given width.
``cut`` ``cut``
Removes all values of arg from the given string Removes all values of arg from the given string.
``date`` ``date``
Formats a date according to the given format (same as the ``now`` tag) Formats a date according to the given format (same as the ``now`` tag).
``default`` ``default``
If value is unavailable, use given default If value is unavailable, use given default.
``default_if_none``
If value is ``None``, use given default.
``dictsort`` ``dictsort``
Takes a list of dicts, returns that list sorted by the property given in the Takes a list of dicts, returns that list sorted by the property given in the
@ -598,24 +601,24 @@ Built-in filter reference
given in the argument. given in the argument.
``divisibleby`` ``divisibleby``
Returns true if the value is divisible by the argument Returns true if the value is divisible by the argument.
``escape`` ``escape``
Escapes a string's HTML Escapes a string's HTML.
``filesizeformat`` ``filesizeformat``
Format the value like a 'human-readable' file size (i.e. 13 KB, 4.1 MB, 102 Format the value like a 'human-readable' file size (i.e. 13 KB, 4.1 MB, 102
bytes, etc). bytes, etc).
``first`` ``first``
Returns the first item in a list Returns the first item in a list.
``fix_ampersands`` ``fix_ampersands``
Replaces ampersands with ``&`` entities Replaces ampersands with ``&`` entities.
``floatformat`` ``floatformat``
Displays a floating point number as 34.2 (with one decimal places) - but Displays a floating point number as 34.2 (with one decimal places) - but
only if there's a point to be displayed only if there's a point to be displayed.
``get_digit`` ``get_digit``
Given a whole number, returns the requested digit of it, where 1 is the Given a whole number, returns the requested digit of it, where 1 is the
@ -624,52 +627,52 @@ Built-in filter reference
or if argument is less than 1). Otherwise, output is always an integer. or if argument is less than 1). Otherwise, output is always an integer.
``join`` ``join``
Joins a list with a string, like Python's ``str.join(list)`` Joins a list with a string, like Python's ``str.join(list)``.
``length`` ``length``
Returns the length of the value - useful for lists Returns the length of the value. Useful for lists.
``length_is`` ``length_is``
Returns a boolean of whether the value's length is the argument Returns a boolean of whether the value's length is the argument.
``linebreaks`` ``linebreaks``
Converts newlines into <p> and <br />s Converts newlines into ``<p>`` and ``<br />``s.
``linebreaksbr`` ``linebreaksbr``
Converts newlines into <br />s Converts newlines into ``<br />``s.
``linenumbers`` ``linenumbers``
Displays text with line numbers Displays text with line numbers.
``ljust`` ``ljust``
Left-aligns the value in a field of a given width Left-aligns the value in a field of a given width.
**Argument:** field size **Argument:** field size
``lower`` ``lower``
Converts a string into all lowercase Converts a string into all lowercase.
``make_list`` ``make_list``
Returns the value turned into a list. For an integer, it's a list of Returns the value turned into a list. For an integer, it's a list of
digits. For a string, it's a list of characters. digits. For a string, it's a list of characters.
``phone2numeric`` ``phone2numeric``
Takes a phone number and converts it in to its numerical equivalent Takes a phone number and converts it in to its numerical equivalent.
``pluralize`` ``pluralize``
Returns 's' if the value is not 1, for '1 vote' vs. '2 votes' Returns 's' if the value is not 1, for '1 vote' vs. '2 votes'.
``pprint`` ``pprint``
A wrapper around pprint.pprint -- for debugging, really A wrapper around pprint.pprint -- for debugging, really.
``random`` ``random``
Returns a random item from the list Returns a random item from the list.
``removetags`` ``removetags``
Removes a space separated list of [X]HTML tags from the output Removes a space separated list of [X]HTML tags from the output.
``rjust`` ``rjust``
Right-aligns the value in a field of a given width Right-aligns the value in a field of a given width.
**Argument:** field size **Argument:** field size
@ -696,19 +699,19 @@ Built-in filter reference
of Python string formatting of Python string formatting
``striptags`` ``striptags``
Strips all [X]HTML tags Strips all [X]HTML tags.
``time`` ``time``
Formats a time according to the given format (same as the ``now`` tag). Formats a time according to the given format (same as the ``now`` tag).
``timesince`` ``timesince``
Formats a date as the time since that date (i.e. "4 days, 6 hours") Formats a date as the time since that date (i.e. "4 days, 6 hours").
``title`` ``title``
Converts a string into titlecase Converts a string into titlecase.
``truncatewords`` ``truncatewords``
Truncates a string after a certain number of words Truncates a string after a certain number of words.
**Argument:** Number of words to truncate after **Argument:** Number of words to truncate after
@ -733,26 +736,27 @@ Built-in filter reference
</li> </li>
``upper`` ``upper``
Converts a string into all uppercase Converts a string into all uppercase.
``urlencode`` ``urlencode``
Escapes a value for use in a URL Escapes a value for use in a URL.
``urlize`` ``urlize``
Converts URLs in plain text into clickable links Converts URLs in plain text into clickable links.
``urlizetrunc`` ``urlizetrunc``
Converts URLs into clickable links, truncating URLs to the given character limit Converts URLs into clickable links, truncating URLs to the given character
limit.
**Argument:** Length to truncate URLs to. **Argument:** Length to truncate URLs to
``wordcount`` ``wordcount``
Returns the number of words Returns the number of words.
``wordwrap`` ``wordwrap``
Wraps words at specified line length Wraps words at specified line length.
**Argument:** number of words to wrap the text at. **Argument:** number of words at which to wrap the text
``yesno`` ``yesno``
Given a string mapping values for true, false and (optionally) None, Given a string mapping values for true, false and (optionally) None,