Django

Code

Changeset 7276

Show
Ignore:
Timestamp:
03/17/08 19:14:41 (6 months ago)
Author:
jacob
Message:

Fixed #6351: added more examples to template filters. Thanks, David Tulig and atlithorn.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/AUTHORS

    r7271 r7276  
    4444    alang@bright-green.com 
    4545    Marty Alchin <gulopine@gamemusic.org> 
     46    atlithorn <atlithorn@gmail.com> 
    4647    Daniel Alves Barbosa de Oliveira Vaz <danielvaz@gmail.com> 
    4748    AgarFu <heaven@croasanaso.sytes.net> 
     
    348349    Makoto Tsuyuki <mtsuyuki@gmail.com> 
    349350    tt@gurgle.no 
     351    David Tulig <david.tulig@gmail.com> 
    350352    Amit Upadhyay 
    351353    Geert Vanderkelen 
  • django/trunk/docs/templates.txt

    r7122 r7276  
    12251225Adds the arg to the value. 
    12261226 
     1227For example:: 
     1228 
     1229    {{ value|add:2 }} 
     1230 
     1231If ``value`` is 4, then the output will be 6. 
     1232 
    12271233addslashes 
    12281234~~~~~~~~~~ 
     
    12481254Removes all values of arg from the given string. 
    12491255 
     1256For example:: 
     1257 
     1258    {{ value|cut:" "}} 
     1259 
     1260If ``value`` is "String with spaces", the output will be ``Stringwithspaces``. 
     1261 
    12501262date 
    12511263~~~~ 
     
    12531265Formats a date according to the given format (same as the `now`_ tag). 
    12541266 
     1267For example:: 
     1268 
     1269    {{ value|date:"D d M Y" }} 
     1270 
     1271If ``value`` is a datetime object (ie. datetime.datetime.now()), the output 
     1272would be formatted like ``Wed 09 Jan 2008``. 
     1273 
    12551274default 
    12561275~~~~~~~ 
     
    12581277If value is unavailable, use given default. 
    12591278 
     1279For example:: 
     1280 
     1281    {{ value|default:"nothing" }} 
     1282 
     1283If ``value`` is ``Undefined``, the output would be ``nothing``. 
     1284 
    12601285default_if_none 
    12611286~~~~~~~~~~~~~~~ 
     
    12631288If value is ``None``, use given default. 
    12641289 
     1290For example:: 
     1291 
     1292    {{ value|default_if_none:"nothing" }} 
     1293 
     1294If ``value`` is ``None``, the output would be ``nothing``. 
     1295 
    12651296dictsort 
    12661297~~~~~~~~ 
     
    12691300the argument. 
    12701301 
     1302For example:: 
     1303 
     1304    {{ value|dictsort:"name" }} 
     1305 
     1306If ``value`` is:: 
     1307     
     1308    [ 
     1309        {'name': 'zed', 'age': 19} 
     1310        {'name': 'amy', 'age': 22},  
     1311        {'name': 'joe', 'age': 31},  
     1312    ] 
     1313 
     1314then the output would be:: 
     1315 
     1316    [ 
     1317        {'name': 'amy', 'age': 22},  
     1318        {'name': 'joe', 'age': 31},  
     1319        {'name': 'zed', 'age': 19} 
     1320    ] 
     1321 
    12711322dictsortreversed 
    12721323~~~~~~~~~~~~~~~~ 
    12731324 
    12741325Takes a list of dictionaries, returns that list sorted in reverse order by the 
    1275 key given in the argument. 
     1326key given in the argument. This works exactly the same as the above filter, but 
     1327the returned value will be in reverse order. 
    12761328 
    12771329divisibleby 
     
    12791331 
    12801332Returns true if the value is divisible by the argument. 
     1333 
     1334For example:: 
     1335 
     1336    {{ value|divisibleby:3 }} 
     1337 
     1338If ``value`` is ``21``, the output would be ``True``. 
    12811339 
    12821340escape 
     
    13201378``'4.1 MB'``, ``'102 bytes'``, etc). 
    13211379 
     1380For example:: 
     1381 
     1382    {{ value|filesizeformat }} 
     1383 
     1384If ``value`` is 123456789, the output would be ``117.7 MB``. 
     1385 
    13221386first 
    13231387~~~~~ 
     
    13251389Returns the first item in a list. 
    13261390 
     1391For example:: 
     1392 
     1393    {{ value|first }} 
     1394 
     1395If ``value`` is ``['a', 'b', 'c']``, the output would be `a`. 
     1396 
    13271397fix_ampersands 
    13281398~~~~~~~~~~~~~~ 
    13291399 
    13301400Replaces ampersands with ``&amp;`` entities. 
     1401 
     1402For example:: 
     1403 
     1404    {{ value|fix_ampersands }} 
     1405 
     1406If ``value`` is ``Tom & Jerry``, the output would be ``Tom &amp; Jerry``. 
     1407 
     1408**New in Django development version**: you probably don't need to use this 
     1409filter since ampersands will be automatically escaped. See escape_ for more on 
     1410how auto-escaping works. 
    13311411 
    13321412floatformat 
     
    13891469is less than 1). Otherwise, output is always an integer. 
    13901470 
     1471For example:: 
     1472     
     1473    {{ value|get_digit:2 }} 
     1474 
     1475If ``value`` is 123456789, the output would be ``8``. 
     1476 
    13911477iriencode 
    13921478~~~~~~~~~ 
     
    14021488~~~~ 
    14031489 
    1404 Joins a list with a string, like Python's ``str.join(list)``. 
     1490Joins a list with a string, like Python's ``str.join(list)`` 
     1491 
     1492For example:: 
     1493 
     1494    {{ value|join:" // " }} 
     1495 
     1496If ``value`` is ``['a', 'b', 'c']``, the output would be ``a // b // c``. 
    14051497 
    14061498last 
     
    14111503Returns the last item in a list. 
    14121504 
     1505For example:: 
     1506 
     1507    {{ value|last }} 
     1508 
     1509If ``value`` is ``['a', 'b', 'c', 'd']``, the output would be ``d``. 
     1510 
    14131511length 
    14141512~~~~~~ 
     
    14161514Returns the length of the value. Useful for lists. 
    14171515 
     1516For example:: 
     1517 
     1518    {{ value|length }} 
     1519 
     1520If ``value`` is ``['a', 'b', 'c', 'd']``, the output would be ``4``. 
     1521 
    14181522length_is 
    14191523~~~~~~~~~ 
    14201524 
    14211525Returns a boolean of whether the value's length is the argument. 
     1526 
     1527For example:: 
     1528 
     1529    {{ value|length_is:4 }} 
     1530 
     1531If ``value`` is ``['a', 'b', 'c', 'd']``, the output would be ``True``. 
    14221532 
    14231533linebreaks 
     
    14281538followed by a blank line becomes a paragraph break (``</p>``). 
    14291539 
     1540For example:: 
     1541 
     1542    {{ value|linebreaks }} 
     1543 
     1544If ``value`` is ``Joel\nis a slug``, the output would be ``<p>Joe<br>is a 
     1545slug</p>``. 
     1546 
    14301547linebreaksbr 
    14311548~~~~~~~~~~~~ 
     
    14511568Converts a string into all lowercase. 
    14521569 
     1570For example:: 
     1571 
     1572    {{ value|lower }} 
     1573 
     1574If ``value`` is ``Joel Is a Slug``, the output would be ``joel is a slug``. 
     1575 
    14531576make_list 
    14541577~~~~~~~~~ 
     
    14561579Returns the value turned into a list. For an integer, it's a list of 
    14571580digits. For a string, it's a list of characters. 
     1581 
     1582For example:: 
     1583 
     1584    {{ value|make_list }} 
     1585 
     1586If ``value`` is "Joe", the output would be ``[u'J', u'o', u'e']. If ``value`` is 
     1587123, the output would be ``[1, 2, 3]``. 
    14581588 
    14591589phone2numeric 
     
    14931623~~~~~~ 
    14941624 
    1495 A wrapper around pprint.pprint -- for debugging, really. 
     1625A wrapper around `pprint.pprint`__ -- for debugging, really. 
     1626 
     1627__ http://www.python.org/doc/2.5/lib/module-pprint.html 
    14961628 
    14971629random 
     
    15001632Returns a random item from the list. 
    15011633 
     1634For example:: 
     1635 
     1636    {{ value|random }} 
     1637 
     1638If ``value`` is ``['a', 'b', 'c', 'd']``, the output could be ``b``. 
     1639 
    15021640removetags 
    15031641~~~~~~~~~~ 
    15041642 
    15051643Removes a space separated list of [X]HTML tags from the output. 
     1644 
     1645For example:: 
     1646 
     1647    {{ value|removetags:"b span"|safe }} 
     1648 
     1649If ``value`` is ``<b>Joel</b> <button>is</button> a <span>slug</span>`` the 
     1650output would be ``Joel <button>is</button> a slug``. 
    15061651 
    15071652rjust 
     
    15361681whitespace. 
    15371682 
     1683For example:: 
     1684 
     1685    {{ value|slugify }} 
     1686 
     1687If ``value`` is ``Joel is a slug``, the output would be ``joel-is-a-slug``. 
     1688 
    15381689stringformat 
    15391690~~~~~~~~~~~~ 
     
    15461697Python string formatting 
    15471698 
     1699For example:: 
     1700 
     1701    {{ value|stringformat:"s" }} 
     1702 
     1703If ``value`` is ``Joel is a slug``, the output would be ``Joel is a slug``. 
     1704 
    15481705striptags 
    15491706~~~~~~~~~ 
    15501707 
    15511708Strips all [X]HTML tags. 
     1709 
     1710For example:: 
     1711 
     1712    {{ value|striptags }} 
     1713 
     1714If ``value`` is ``<b>Joel</b> <button>is</button> a <span>slug</span>`` the 
     1715output would be ``Joel is a slug``. 
    15521716 
    15531717time 
     
    15581722to the time of day, not the date (for obvious reasons). If you need to 
    15591723format a date, use the `date`_ filter. 
     1724 
     1725For example:: 
     1726 
     1727    {{ value|time:"H:i" }} 
     1728 
     1729If ``value`` is ``datetime.datetime.now()``, the output would be formatted 
     1730like ``01:23``. 
    15601731 
    15611732timesince 
     
    15991770 
    16001771**Argument:** Number of words to truncate after 
     1772 
     1773For example:: 
     1774 
     1775    {{ value|truncatewords:2 }} 
     1776 
     1777If ``value`` is ``Joel is a slug``, the output would be ``Joel is ...``. 
    16011778 
    16021779truncatewords_html 
     
    16451822Converts a string into all uppercase. 
    16461823 
     1824For example:: 
     1825 
     1826    {{ value|upper }} 
     1827 
     1828If ``value`` is ``Joel is a slug``, the output would be ``JOEL IS A SLUG``. 
     1829 
    16471830urlencode 
    16481831~~~~~~~~~ 
     
    16581841things won't work as expected. Apply this filter only to *plain* text. 
    16591842 
     1843For example:: 
     1844 
     1845    {{ value|urlize }} 
     1846 
     1847If ``value`` is ``Check out www.djangoproject.com``, the output would be 
     1848``Check out <a 
     1849href="http://www.djangoproject.com">www.djangoproject.com</a>``. 
     1850 
    16601851urlizetrunc 
    16611852~~~~~~~~~~~ 
     
    16681859**Argument:** Length to truncate URLs to 
    16691860 
     1861For example:: 
     1862 
     1863    {{ value|urlizetrunc:15 }} 
     1864 
     1865If ``value`` is ``Check out www.djangoproject.com``, the output would be 
     1866``Check out <a 
     1867href="http://www.djangoproject.com">www.djangopr...</a>``. 
     1868 
    16701869wordcount 
    16711870~~~~~~~~~ 
     
    16791878 
    16801879**Argument:** number of characters at which to wrap the text 
     1880 
     1881For example:: 
     1882 
     1883    {{ value|wordwrap:5 }} 
     1884 
     1885If ``value`` is ``Joel is a slug``, the output would be:: 
     1886 
     1887    Joel 
     1888    is a 
     1889    slug 
    16811890 
    16821891yesno