Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #12496 - Added code examples to built-in filter documentation. …

…Thanks, Arthur Koziel.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@12220 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit e752d4984aa77196335321359b4492a11ea70f8e 1 parent f499857
Jannis Leidel authored January 12, 2010

Showing 1 changed file with 104 additions and 2 deletions. Show diff stats Hide diff stats

  1. 106  docs/ref/templates/builtins.txt
106  docs/ref/templates/builtins.txt
@@ -1017,6 +1017,12 @@ addslashes
1017 1017
 
1018 1018
 Adds slashes before quotes. Useful for escaping strings in CSV, for example.
1019 1019
 
  1020
+For example::
  1021
+
  1022
+    {{ value|addslashes }}
  1023
+
  1024
+If ``value`` is ``"I'm using Django"``, the output will be ``"I\'m using Django"``.
  1025
+
1020 1026
 .. templatefilter:: capfirst
1021 1027
 
1022 1028
 capfirst
@@ -1024,6 +1030,12 @@ capfirst
1024 1030
 
1025 1031
 Capitalizes the first character of the value.
1026 1032
 
  1033
+For example::
  1034
+
  1035
+    {{ value|capfirst }}
  1036
+
  1037
+If ``value`` is ``"django"``, the output will be ``"Django"``.
  1038
+
1027 1039
 .. templatefilter:: center
1028 1040
 
1029 1041
 center
@@ -1031,6 +1043,12 @@ center
1031 1043
 
1032 1044
 Centers the value in a field of a given width.
1033 1045
 
  1046
+For example::
  1047
+
  1048
+    "{{ value|center:"15" }}"
  1049
+
  1050
+If ``value`` is ``"Django"``, the output will be ``"     Django    "``.
  1051
+
1034 1052
 .. templatefilter:: cut
1035 1053
 
1036 1054
 cut
@@ -1194,6 +1212,13 @@ Escapes characters for use in JavaScript strings. This does *not* make the
1194 1212
 string safe for use in HTML, but does protect you from syntax errors when using
1195 1213
 templates to generate JavaScript/JSON.
1196 1214
 
  1215
+For example::
  1216
+
  1217
+    {{ value|escapejs }}
  1218
+
  1219
+If ``value`` is ``"testing\r\njavascript \'string" <b>escaping</b>"``,
  1220
+the output will be ``"testing\\x0D\\x0Ajavascript \\x27string\\x22 \\x3Cb\\x3Eescaping\\x3C/b\\x3E"``.
  1221
+
1197 1222
 .. templatefilter:: filesizeformat
1198 1223
 
1199 1224
 filesizeformat
@@ -1320,6 +1345,12 @@ strings containing non-ASCII characters in a URL.
1320 1345
 It's safe to use this filter on a string that has already gone through the
1321 1346
 ``urlencode`` filter.
1322 1347
 
  1348
+For example::
  1349
+
  1350
+    {{ value|iriencode }}
  1351
+
  1352
+If ``value`` is ``"?test=1&me=2"``, the output will be ``"?test=1&amp;me=2"``.
  1353
+
1323 1354
 .. templatefilter:: join
1324 1355
 
1325 1356
 join
@@ -1400,6 +1431,13 @@ linebreaksbr
1400 1431
 Converts all newlines in a piece of plain text to HTML line breaks
1401 1432
 (``<br />``).
1402 1433
 
  1434
+For example::
  1435
+
  1436
+    {{ value|linebreaksbr }}
  1437
+
  1438
+If ``value`` is ``Joel\nis a slug``, the output will be ``Joel<br />is a
  1439
+slug``.
  1440
+
1403 1441
 .. templatefilter:: linenumbers
1404 1442
 
1405 1443
 linenumbers
@@ -1407,6 +1445,22 @@ linenumbers
1407 1445
 
1408 1446
 Displays text with line numbers.
1409 1447
 
  1448
+For example::
  1449
+
  1450
+    {{ value|linenumbers }}
  1451
+
  1452
+If ``value`` is::
  1453
+
  1454
+    one
  1455
+    two
  1456
+    three
  1457
+
  1458
+the output will be::
  1459
+
  1460
+    1. one
  1461
+    2. two
  1462
+    3. three
  1463
+
1410 1464
 .. templatefilter:: ljust
1411 1465
 
1412 1466
 ljust
@@ -1416,6 +1470,12 @@ Left-aligns the value in a field of a given width.
1416 1470
 
1417 1471
 **Argument:** field size
1418 1472
 
  1473
+For example::
  1474
+
  1475
+    "{{ value|ljust:"10" }}"
  1476
+
  1477
+If ``value`` is ``Django``, the output will be ``"Django    "``.
  1478
+
1419 1479
 .. templatefilter:: lower
1420 1480
 
1421 1481
 lower
@@ -1451,12 +1511,17 @@ phone2numeric
1451 1511
 ~~~~~~~~~~~~~
1452 1512
 
1453 1513
 Converts a phone number (possibly containing letters) to its numerical
1454  
-equivalent. For example, ``'800-COLLECT'`` will be converted to
1455  
-``'800-2655328'``.
  1514
+equivalent.
1456 1515
 
1457 1516
 The input doesn't have to be a valid phone number. This will happily convert
1458 1517
 any string.
1459 1518
 
  1519
+For example::
  1520
+
  1521
+    {{ value|phone2numeric }}
  1522
+
  1523
+If ``value`` is ``800-COLLECT``, the output will be ``800-2655328``.
  1524
+
1460 1525
 .. templatefilter:: pluralize
1461 1526
 
1462 1527
 pluralize
@@ -1468,6 +1533,9 @@ Example::
1468 1533
 
1469 1534
     You have {{ num_messages }} message{{ num_messages|pluralize }}.
1470 1535
 
  1536
+If ``num_messages`` is ``1``, the output will be ``You have 1 message.``
  1537
+If ``num_messages`` is ``2``  the output will be ``You have 2 messages.``
  1538
+
1471 1539
 For words that require a suffix other than ``'s'``, you can provide an alternate
1472 1540
 suffix as a parameter to the filter.
1473 1541
 
@@ -1527,6 +1595,12 @@ Right-aligns the value in a field of a given width.
1527 1595
 
1528 1596
 **Argument:** field size
1529 1597
 
  1598
+For example::
  1599
+
  1600
+    "{{ value|rjust:"10" }}"
  1601
+
  1602
+If ``value`` is ``Django``, the output will be ``"    Django"``.
  1603
+
1530 1604
 .. templatefilter:: safe
1531 1605
 
1532 1606
 safe
@@ -1565,6 +1639,8 @@ Example::
1565 1639
 
1566 1640
     {{ some_list|slice:":2" }}
1567 1641
 
  1642
+If ``some_list`` is ``['a', 'b', 'c']``, the output will be ``['a', 'b']``.
  1643
+
1568 1644
 .. templatefilter:: slugify
1569 1645
 
1570 1646
 slugify
@@ -1684,6 +1760,12 @@ title
1684 1760
 
1685 1761
 Converts a string into titlecase.
1686 1762
 
  1763
+For example::
  1764
+
  1765
+    {{ value|title }}
  1766
+
  1767
+If ``value`` is ``"my first post"``, the output will be ``"My First Post"``.
  1768
+
1687 1769
 .. templatefilter:: truncatewords
1688 1770
 
1689 1771
 truncatewords
@@ -1711,6 +1793,13 @@ closed immediately after the truncation.
1711 1793
 This is less efficient than ``truncatewords``, so should only be used when it
1712 1794
 is being passed HTML text.
1713 1795
 
  1796
+For example::
  1797
+
  1798
+    {{ value|truncatewords_html:2 }}
  1799
+
  1800
+If ``value`` is ``"<p>Joel is a slug</p>"``, the output will be
  1801
+``"<p>Joel is ...</p>"``.
  1802
+
1714 1803
 .. templatefilter:: unordered_list
1715 1804
 
1716 1805
 unordered_list
@@ -1761,6 +1850,13 @@ urlencode
1761 1850
 
1762 1851
 Escapes a value for use in a URL.
1763 1852
 
  1853
+For example::
  1854
+
  1855
+    {{ value|urlencode }}
  1856
+
  1857
+If ``value`` is ``"http://www.example.org/foo?a=b&c=d"``, the output will be
  1858
+``"http%3A//www.example.org/foo%3Fa%3Db%26c%3Dd"``.
  1859
+
1764 1860
 .. templatefilter:: urlize
1765 1861
 
1766 1862
 urlize
@@ -1806,6 +1902,12 @@ wordcount
1806 1902
 
1807 1903
 Returns the number of words.
1808 1904
 
  1905
+For example::
  1906
+
  1907
+    {{ value|wordcount }}
  1908
+
  1909
+If ``value`` is ``"Joel is a slug"``, the output will be ``4``.
  1910
+
1809 1911
 .. templatefilter:: wordwrap
1810 1912
 
1811 1913
 wordwrap

0 notes on commit e752d49

Please sign in to comment.
Something went wrong with that request. Please try again.