Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[1.1.X] Fixed #12496 - Added code examples to built-in filter documen…

…tation. Thanks, Arthur Koziel.

git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.1.X@12243 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 0c4f8b345f04c4fe8b07f42c5f846f43ee3c05fb 1 parent dc3d26b
Jannis Leidel authored January 17, 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
@@ -876,6 +876,12 @@ addslashes
876 876
 
877 877
 Adds slashes before quotes. Useful for escaping strings in CSV, for example.
878 878
 
  879
+For example::
  880
+
  881
+    {{ value|addslashes }}
  882
+
  883
+If ``value`` is ``"I'm using Django"``, the output will be ``"I\'m using Django"``.
  884
+
879 885
 .. templatefilter:: capfirst
880 886
 
881 887
 capfirst
@@ -883,6 +889,12 @@ capfirst
883 889
 
884 890
 Capitalizes the first character of the value.
885 891
 
  892
+For example::
  893
+
  894
+    {{ value|capfirst }}
  895
+
  896
+If ``value`` is ``"django"``, the output will be ``"Django"``.
  897
+
886 898
 .. templatefilter:: center
887 899
 
888 900
 center
@@ -890,6 +902,12 @@ center
890 902
 
891 903
 Centers the value in a field of a given width.
892 904
 
  905
+For example::
  906
+
  907
+    "{{ value|center:"15" }}"
  908
+
  909
+If ``value`` is ``"Django"``, the output will be ``"     Django    "``.
  910
+
893 911
 .. templatefilter:: cut
894 912
 
895 913
 cut
@@ -1048,6 +1066,13 @@ Escapes characters for use in JavaScript strings. This does *not* make the
1048 1066
 string safe for use in HTML, but does protect you from syntax errors when using
1049 1067
 templates to generate JavaScript/JSON.
1050 1068
 
  1069
+For example::
  1070
+
  1071
+    {{ value|escapejs }}
  1072
+
  1073
+If ``value`` is ``"testing\r\njavascript \'string" <b>escaping</b>"``,
  1074
+the output will be ``"testing\\x0D\\x0Ajavascript \\x27string\\x22 \\x3Cb\\x3Eescaping\\x3C/b\\x3E"``.
  1075
+
1051 1076
 .. templatefilter:: filesizeformat
1052 1077
 
1053 1078
 filesizeformat
@@ -1174,6 +1199,12 @@ strings containing non-ASCII characters in a URL.
1174 1199
 It's safe to use this filter on a string that has already gone through the
1175 1200
 ``urlencode`` filter.
1176 1201
 
  1202
+For example::
  1203
+
  1204
+    {{ value|iriencode }}
  1205
+
  1206
+If ``value`` is ``"?test=1&me=2"``, the output will be ``"?test=1&amp;me=2"``.
  1207
+
1177 1208
 .. templatefilter:: join
1178 1209
 
1179 1210
 join
@@ -1254,6 +1285,13 @@ linebreaksbr
1254 1285
 Converts all newlines in a piece of plain text to HTML line breaks
1255 1286
 (``<br />``).
1256 1287
 
  1288
+For example::
  1289
+
  1290
+    {{ value|linebreaksbr }}
  1291
+
  1292
+If ``value`` is ``Joel\nis a slug``, the output will be ``Joel<br />is a
  1293
+slug``.
  1294
+
1257 1295
 .. templatefilter:: linenumbers
1258 1296
 
1259 1297
 linenumbers
@@ -1261,6 +1299,22 @@ linenumbers
1261 1299
 
1262 1300
 Displays text with line numbers.
1263 1301
 
  1302
+For example::
  1303
+
  1304
+    {{ value|linenumbers }}
  1305
+
  1306
+If ``value`` is::
  1307
+
  1308
+    one
  1309
+    two
  1310
+    three
  1311
+
  1312
+the output will be::
  1313
+
  1314
+    1. one
  1315
+    2. two
  1316
+    3. three
  1317
+
1264 1318
 .. templatefilter:: ljust
1265 1319
 
1266 1320
 ljust
@@ -1270,6 +1324,12 @@ Left-aligns the value in a field of a given width.
1270 1324
 
1271 1325
 **Argument:** field size
1272 1326
 
  1327
+For example::
  1328
+
  1329
+    "{{ value|ljust:"10" }}"
  1330
+
  1331
+If ``value`` is ``Django``, the output will be ``"Django    "``.
  1332
+
1273 1333
 .. templatefilter:: lower
1274 1334
 
1275 1335
 lower
@@ -1305,12 +1365,17 @@ phone2numeric
1305 1365
 ~~~~~~~~~~~~~
1306 1366
 
1307 1367
 Converts a phone number (possibly containing letters) to its numerical
1308  
-equivalent. For example, ``'800-COLLECT'`` will be converted to
1309  
-``'800-2655328'``.
  1368
+equivalent.
1310 1369
 
1311 1370
 The input doesn't have to be a valid phone number. This will happily convert
1312 1371
 any string.
1313 1372
 
  1373
+For example::
  1374
+
  1375
+    {{ value|phone2numeric }}
  1376
+
  1377
+If ``value`` is ``800-COLLECT``, the output will be ``800-2655328``.
  1378
+
1314 1379
 .. templatefilter:: pluralize
1315 1380
 
1316 1381
 pluralize
@@ -1322,6 +1387,9 @@ Example::
1322 1387
 
1323 1388
     You have {{ num_messages }} message{{ num_messages|pluralize }}.
1324 1389
 
  1390
+If ``num_messages`` is ``1``, the output will be ``You have 1 message.``
  1391
+If ``num_messages`` is ``2``  the output will be ``You have 2 messages.``
  1392
+
1325 1393
 For words that require a suffix other than ``'s'``, you can provide an alternate
1326 1394
 suffix as a parameter to the filter.
1327 1395
 
@@ -1381,6 +1449,12 @@ Right-aligns the value in a field of a given width.
1381 1449
 
1382 1450
 **Argument:** field size
1383 1451
 
  1452
+For example::
  1453
+
  1454
+    "{{ value|rjust:"10" }}"
  1455
+
  1456
+If ``value`` is ``Django``, the output will be ``"    Django"``.
  1457
+
1384 1458
 .. templatefilter:: safe
1385 1459
 
1386 1460
 safe
@@ -1419,6 +1493,8 @@ Example::
1419 1493
 
1420 1494
     {{ some_list|slice:":2" }}
1421 1495
 
  1496
+If ``some_list`` is ``['a', 'b', 'c']``, the output will be ``['a', 'b']``.
  1497
+
1422 1498
 .. templatefilter:: slugify
1423 1499
 
1424 1500
 slugify
@@ -1534,6 +1610,12 @@ title
1534 1610
 
1535 1611
 Converts a string into titlecase.
1536 1612
 
  1613
+For example::
  1614
+
  1615
+    {{ value|title }}
  1616
+
  1617
+If ``value`` is ``"my first post"``, the output will be ``"My First Post"``.
  1618
+
1537 1619
 .. templatefilter:: truncatewords
1538 1620
 
1539 1621
 truncatewords
@@ -1561,6 +1643,13 @@ closed immediately after the truncation.
1561 1643
 This is less efficient than ``truncatewords``, so should only be used when it
1562 1644
 is being passed HTML text.
1563 1645
 
  1646
+For example::
  1647
+
  1648
+    {{ value|truncatewords_html:2 }}
  1649
+
  1650
+If ``value`` is ``"<p>Joel is a slug</p>"``, the output will be
  1651
+``"<p>Joel is ...</p>"``.
  1652
+
1564 1653
 .. templatefilter:: unordered_list
1565 1654
 
1566 1655
 unordered_list
@@ -1611,6 +1700,13 @@ urlencode
1611 1700
 
1612 1701
 Escapes a value for use in a URL.
1613 1702
 
  1703
+For example::
  1704
+
  1705
+    {{ value|urlencode }}
  1706
+
  1707
+If ``value`` is ``"http://www.example.org/foo?a=b&c=d"``, the output will be
  1708
+``"http%3A//www.example.org/foo%3Fa%3Db%26c%3Dd"``.
  1709
+
1614 1710
 .. templatefilter:: urlize
1615 1711
 
1616 1712
 urlize
@@ -1656,6 +1752,12 @@ wordcount
1656 1752
 
1657 1753
 Returns the number of words.
1658 1754
 
  1755
+For example::
  1756
+
  1757
+    {{ value|wordcount }}
  1758
+
  1759
+If ``value`` is ``"Joel is a slug"``, the output will be ``4``.
  1760
+
1659 1761
 .. templatefilter:: wordwrap
1660 1762
 
1661 1763
 wordwrap

0 notes on commit 0c4f8b3

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