builtins.txt 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589
  1. .. _ref-templates-builtins:
  2. ==================================
  3. Built-in template tags and filters
  4. ==================================
  5. This document describes Django's built-in template tags and filters. It is
  6. recommended that you use the :ref:`automatic documentation
  7. <template-built-in-reference>`, if available, as this will also include
  8. documentation for any custom tags or filters installed.
  9. .. _ref-templates-builtins-tags:
  10. Built-in tag reference
  11. ----------------------
  12. .. highlightlang:: html+django
  13. .. templatetag:: autoescape
  14. autoescape
  15. ~~~~~~~~~~
  16. .. versionadded:: 1.0
  17. Control the current auto-escaping behavior. This tag takes either ``on`` or
  18. ``off`` as an argument and that determines whether auto-escaping is in effect
  19. inside the block.
  20. When auto-escaping is in effect, all variable content has HTML escaping applied
  21. to it before placing the result into the output (but after any filters have
  22. been applied). This is equivalent to manually applying the ``escape`` filter
  23. to each variable.
  24. The only exceptions are variables that are already marked as "safe" from
  25. escaping, either by the code that populated the variable, or because it has had
  26. the ``safe`` or ``escape`` filters applied.
  27. .. templatetag:: block
  28. block
  29. ~~~~~
  30. Define a block that can be overridden by child templates. See
  31. :ref:`Template inheritance <template-inheritance>` for more information.
  32. .. templatetag:: comment
  33. comment
  34. ~~~~~~~
  35. Ignore everything between ``{% comment %}`` and ``{% endcomment %}``
  36. .. templatetag:: cycle
  37. cycle
  38. ~~~~~
  39. .. versionchanged:: 1.0
  40. Cycle among the given strings or variables each time this tag is encountered.
  41. Within a loop, cycles among the given strings/variables each time through the
  42. loop::
  43. {% for o in some_list %}
  44. <tr class="{% cycle 'row1' 'row2' rowvar %}">
  45. ...
  46. </tr>
  47. {% endfor %}
  48. Outside of a loop, give the values a unique name the first time you call it,
  49. then use that name each successive time through::
  50. <tr class="{% cycle 'row1' 'row2' rowvar as rowcolors %}">...</tr>
  51. <tr class="{% cycle rowcolors %}">...</tr>
  52. <tr class="{% cycle rowcolors %}">...</tr>
  53. You can use any number of values, separated by spaces. Values enclosed in
  54. single (') or double quotes (") are treated as string literals, while values
  55. without quotes are assumed to refer to context variables.
  56. You can also separate values with commas::
  57. {% cycle row1,row2,row3 %}
  58. In this syntax, each value will be interpreted as literal text. The
  59. comma-based syntax exists for backwards-compatibility, and should not be
  60. used for new projects.
  61. .. templatetag:: debug
  62. debug
  63. ~~~~~
  64. Output a whole load of debugging information, including the current context and
  65. imported modules.
  66. .. templatetag:: extends
  67. extends
  68. ~~~~~~~
  69. Signal that this template extends a parent template.
  70. This tag can be used in two ways:
  71. * ``{% extends "base.html" %}`` (with quotes) uses the literal value
  72. ``"base.html"`` as the name of the parent template to extend.
  73. * ``{% extends variable %}`` uses the value of ``variable``. If the variable
  74. evaluates to a string, Django will use that string as the name of the
  75. parent template. If the variable evaluates to a ``Template`` object,
  76. Django will use that object as the parent template.
  77. See :ref:`template-inheritance` for more information.
  78. .. templatetag:: filter
  79. filter
  80. ~~~~~~
  81. Filter the contents of the variable through variable filters.
  82. Filters can also be piped through each other, and they can have arguments --
  83. just like in variable syntax.
  84. Sample usage::
  85. {% filter force_escape|lower %}
  86. This text will be HTML-escaped, and will appear in all lowercase.
  87. {% endfilter %}
  88. .. templatetag:: firstof
  89. firstof
  90. ~~~~~~~
  91. Outputs the first variable passed that is not False. Outputs nothing if all the
  92. passed variables are False.
  93. Sample usage::
  94. {% firstof var1 var2 var3 %}
  95. This is equivalent to::
  96. {% if var1 %}
  97. {{ var1 }}
  98. {% else %}{% if var2 %}
  99. {{ var2 }}
  100. {% else %}{% if var3 %}
  101. {{ var3 }}
  102. {% endif %}{% endif %}{% endif %}
  103. You can also use a literal string as a fallback value in case all
  104. passed variables are False::
  105. {% firstof var1 var2 var3 "fallback value" %}
  106. .. templatetag:: for
  107. for
  108. ~~~
  109. Loop over each item in an array. For example, to display a list of athletes
  110. provided in ``athlete_list``::
  111. <ul>
  112. {% for athlete in athlete_list %}
  113. <li>{{ athlete.name }}</li>
  114. {% endfor %}
  115. </ul>
  116. You can loop over a list in reverse by using ``{% for obj in list reversed %}``.
  117. .. versionadded:: 1.0
  118. If you need to loop over a list of lists, you can unpack the values
  119. in each sub-list into individual variables. For example, if your context
  120. contains a list of (x,y) coordinates called ``points``, you could use the
  121. following to output the list of points::
  122. {% for x, y in points %}
  123. There is a point at {{ x }},{{ y }}
  124. {% endfor %}
  125. This can also be useful if you need to access the items in a dictionary.
  126. For example, if your context contained a dictionary ``data``, the following
  127. would display the keys and values of the dictionary::
  128. {% for key, value in data.items %}
  129. {{ key }}: {{ value }}
  130. {% endfor %}
  131. The for loop sets a number of variables available within the loop:
  132. ========================== ================================================
  133. Variable Description
  134. ========================== ================================================
  135. ``forloop.counter`` The current iteration of the loop (1-indexed)
  136. ``forloop.counter0`` The current iteration of the loop (0-indexed)
  137. ``forloop.revcounter`` The number of iterations from the end of the
  138. loop (1-indexed)
  139. ``forloop.revcounter0`` The number of iterations from the end of the
  140. loop (0-indexed)
  141. ``forloop.first`` True if this is the first time through the loop
  142. ``forloop.last`` True if this is the last time through the loop
  143. ``forloop.parentloop`` For nested loops, this is the loop "above" the
  144. current one
  145. ========================== ================================================
  146. .. templatetag:: if
  147. if
  148. ~~
  149. The ``{% if %}`` tag evaluates a variable, and if that variable is "true" (i.e.
  150. exists, is not empty, and is not a false boolean value) the contents of the
  151. block are output::
  152. {% if athlete_list %}
  153. Number of athletes: {{ athlete_list|length }}
  154. {% else %}
  155. No athletes.
  156. {% endif %}
  157. In the above, if ``athlete_list`` is not empty, the number of athletes will be
  158. displayed by the ``{{ athlete_list|length }}`` variable.
  159. As you can see, the ``if`` tag can take an optional ``{% else %}`` clause that
  160. will be displayed if the test fails.
  161. ``if`` tags may use ``and``, ``or`` or ``not`` to test a number of variables or
  162. to negate a given variable::
  163. {% if athlete_list and coach_list %}
  164. Both athletes and coaches are available.
  165. {% endif %}
  166. {% if not athlete_list %}
  167. There are no athletes.
  168. {% endif %}
  169. {% if athlete_list or coach_list %}
  170. There are some athletes or some coaches.
  171. {% endif %}
  172. {% if not athlete_list or coach_list %}
  173. There are no athletes or there are some coaches (OK, so
  174. writing English translations of boolean logic sounds
  175. stupid; it's not our fault).
  176. {% endif %}
  177. {% if athlete_list and not coach_list %}
  178. There are some athletes and absolutely no coaches.
  179. {% endif %}
  180. ``if`` tags don't allow ``and`` and ``or`` clauses within the same tag, because
  181. the order of logic would be ambiguous. For example, this is invalid::
  182. {% if athlete_list and coach_list or cheerleader_list %}
  183. If you need to combine ``and`` and ``or`` to do advanced logic, just use nested
  184. ``if`` tags. For example::
  185. {% if athlete_list %}
  186. {% if coach_list or cheerleader_list %}
  187. We have athletes, and either coaches or cheerleaders!
  188. {% endif %}
  189. {% endif %}
  190. Multiple uses of the same logical operator are fine, as long as you use the
  191. same operator. For example, this is valid::
  192. {% if athlete_list or coach_list or parent_list or teacher_list %}
  193. .. templatetag:: ifchanged
  194. ifchanged
  195. ~~~~~~~~~
  196. Check if a value has changed from the last iteration of a loop.
  197. The 'ifchanged' block tag is used within a loop. It has two possible uses.
  198. 1. Checks its own rendered contents against its previous state and only
  199. displays the content if it has changed. For example, this displays a list of
  200. days, only displaying the month if it changes::
  201. <h1>Archive for {{ year }}</h1>
  202. {% for date in days %}
  203. {% ifchanged %}<h3>{{ date|date:"F" }}</h3>{% endifchanged %}
  204. <a href="{{ date|date:"M/d"|lower }}/">{{ date|date:"j" }}</a>
  205. {% endfor %}
  206. 2. If given a variable, check whether that variable has changed. For
  207. example, the following shows the date every time it changes, but
  208. only shows the hour if both the hour and the date has changed::
  209. {% for date in days %}
  210. {% ifchanged date.date %} {{ date.date }} {% endifchanged %}
  211. {% ifchanged date.hour date.date %}
  212. {{ date.hour }}
  213. {% endifchanged %}
  214. {% endfor %}
  215. .. templatetag:: ifequal
  216. ifequal
  217. ~~~~~~~
  218. Output the contents of the block if the two arguments equal each other.
  219. Example::
  220. {% ifequal user.id comment.user_id %}
  221. ...
  222. {% endifequal %}
  223. As in the ``{% if %}`` tag, an ``{% else %}`` clause is optional.
  224. The arguments can be hard-coded strings, so the following is valid::
  225. {% ifequal user.username "adrian" %}
  226. ...
  227. {% endifequal %}
  228. It is only possible to compare an argument to template variables or strings.
  229. You cannot check for equality with Python objects such as ``True`` or
  230. ``False``. If you need to test if something is true or false, use the ``if``
  231. tag instead.
  232. .. templatetag:: ifnotequal
  233. ifnotequal
  234. ~~~~~~~~~~
  235. Just like ``ifequal``, except it tests that the two arguments are not equal.
  236. .. templatetag:: include
  237. include
  238. ~~~~~~~
  239. Loads a template and renders it with the current context. This is a way of
  240. "including" other templates within a template.
  241. The template name can either be a variable or a hard-coded (quoted) string,
  242. in either single or double quotes.
  243. This example includes the contents of the template ``"foo/bar.html"``::
  244. {% include "foo/bar.html" %}
  245. This example includes the contents of the template whose name is contained in
  246. the variable ``template_name``::
  247. {% include template_name %}
  248. An included template is rendered with the context of the template that's
  249. including it. This example produces the output ``"Hello, John"``:
  250. * Context: variable ``person`` is set to ``"john"``.
  251. * Template::
  252. {% include "name_snippet.html" %}
  253. * The ``name_snippet.html`` template::
  254. Hello, {{ person }}
  255. See also: ``{% ssi %}``.
  256. .. templatetag:: load
  257. load
  258. ~~~~
  259. Load a custom template tag set.
  260. See :ref:`Custom tag and filter libraries <howto-custom-template-tags>` for more information.
  261. .. templatetag:: now
  262. now
  263. ~~~
  264. Display the date, formatted according to the given string.
  265. Uses the same format as PHP's ``date()`` function (http://php.net/date)
  266. with some custom extensions.
  267. Available format strings:
  268. ================ ======================================== =====================
  269. Format character Description Example output
  270. ================ ======================================== =====================
  271. a ``'a.m.'`` or ``'p.m.'`` (Note that ``'a.m.'``
  272. this is slightly different than PHP's
  273. output, because this includes periods
  274. to match Associated Press style.)
  275. A ``'AM'`` or ``'PM'``. ``'AM'``
  276. b Month, textual, 3 letters, lowercase. ``'jan'``
  277. B Not implemented.
  278. d Day of the month, 2 digits with ``'01'`` to ``'31'``
  279. leading zeros.
  280. D Day of the week, textual, 3 letters. ``'Fri'``
  281. f Time, in 12-hour hours and minutes, ``'1'``, ``'1:30'``
  282. with minutes left off if they're zero.
  283. Proprietary extension.
  284. F Month, textual, long. ``'January'``
  285. g Hour, 12-hour format without leading ``'1'`` to ``'12'``
  286. zeros.
  287. G Hour, 24-hour format without leading ``'0'`` to ``'23'``
  288. zeros.
  289. h Hour, 12-hour format. ``'01'`` to ``'12'``
  290. H Hour, 24-hour format. ``'00'`` to ``'23'``
  291. i Minutes. ``'00'`` to ``'59'``
  292. I Not implemented.
  293. j Day of the month without leading ``'1'`` to ``'31'``
  294. zeros.
  295. l Day of the week, textual, long. ``'Friday'``
  296. L Boolean for whether it's a leap year. ``True`` or ``False``
  297. m Month, 2 digits with leading zeros. ``'01'`` to ``'12'``
  298. M Month, textual, 3 letters. ``'Jan'``
  299. n Month without leading zeros. ``'1'`` to ``'12'``
  300. N Month abbreviation in Associated Press ``'Jan.'``, ``'Feb.'``, ``'March'``, ``'May'``
  301. style. Proprietary extension.
  302. O Difference to Greenwich time in hours. ``'+0200'``
  303. P Time, in 12-hour hours, minutes and ``'1 a.m.'``, ``'1:30 p.m.'``, ``'midnight'``, ``'noon'``, ``'12:30 p.m.'``
  304. 'a.m.'/'p.m.', with minutes left off
  305. if they're zero and the special-case
  306. strings 'midnight' and 'noon' if
  307. appropriate. Proprietary extension.
  308. r RFC 2822 formatted date. ``'Thu, 21 Dec 2000 16:01:07 +0200'``
  309. s Seconds, 2 digits with leading zeros. ``'00'`` to ``'59'``
  310. S English ordinal suffix for day of the ``'st'``, ``'nd'``, ``'rd'`` or ``'th'``
  311. month, 2 characters.
  312. t Number of days in the given month. ``28`` to ``31``
  313. T Time zone of this machine. ``'EST'``, ``'MDT'``
  314. U Not implemented.
  315. w Day of the week, digits without ``'0'`` (Sunday) to ``'6'`` (Saturday)
  316. leading zeros.
  317. W ISO-8601 week number of year, with ``1``, ``53``
  318. weeks starting on Monday.
  319. y Year, 2 digits. ``'99'``
  320. Y Year, 4 digits. ``'1999'``
  321. z Day of the year. ``0`` to ``365``
  322. Z Time zone offset in seconds. The ``-43200`` to ``43200``
  323. offset for timezones west of UTC is
  324. always negative, and for those east of
  325. UTC is always positive.
  326. ================ ======================================== =====================
  327. Example::
  328. It is {% now "jS F Y H:i" %}
  329. Note that you can backslash-escape a format string if you want to use the
  330. "raw" value. In this example, "f" is backslash-escaped, because otherwise
  331. "f" is a format string that displays the time. The "o" doesn't need to be
  332. escaped, because it's not a format character::
  333. It is the {% now "jS o\f F" %}
  334. This would display as "It is the 4th of September".
  335. .. templatetag:: regroup
  336. regroup
  337. ~~~~~~~
  338. Regroup a list of alike objects by a common attribute.
  339. This complex tag is best illustrated by use of an example: say that ``people``
  340. is a list of people represented by dictionaries with ``first_name``,
  341. ``last_name``, and ``gender`` keys:
  342. .. code-block:: python
  343. people = [
  344. {'first_name': 'George', 'last_name': 'Bush', 'gender': 'Male'},
  345. {'first_name': 'Bill', 'last_name': 'Clinton', 'gender': 'Male'},
  346. {'first_name': 'Margaret', 'last_name': 'Thatcher', 'gender': 'Female'},
  347. {'first_name': 'Condoleezza', 'last_name': 'Rice', 'gender': 'Female'},
  348. {'first_name': 'Pat', 'last_name': 'Smith', 'gender': 'Unknown'},
  349. ]
  350. ...and you'd like to display a hierarchical list that is ordered by gender,
  351. like this:
  352. * Male:
  353. * George Bush
  354. * Bill Clinton
  355. * Female:
  356. * Margaret Thatcher
  357. * Condoleezza Rice
  358. * Unknown:
  359. * Pat Smith
  360. You can use the ``{% regroup %}`` tag to group the list of people by gender.
  361. The following snippet of template code would accomplish this::
  362. {% regroup people by gender as gender_list %}
  363. <ul>
  364. {% for gender in gender_list %}
  365. <li>{{ gender.grouper }}
  366. <ul>
  367. {% for item in gender.list %}
  368. <li>{{ item.first_name }} {{ item.last_name }}</li>
  369. {% endfor %}
  370. </ul>
  371. </li>
  372. {% endfor %}
  373. </ul>
  374. Let's walk through this example. ``{% regroup %}`` takes three arguments: the
  375. list you want to regroup, the attribute to group by, and the name of the
  376. resulting list. Here, we're regrouping the ``people`` list by the ``gender``
  377. attribute and calling the result ``gender_list``.
  378. ``{% regroup %}`` produces a list (in this case, ``gender_list``) of
  379. **group objects**. Each group object has two attributes:
  380. * ``grouper`` -- the item that was grouped by (e.g., the string "Male" or
  381. "Female").
  382. * ``list`` -- a list of all items in this group (e.g., a list of all people
  383. with gender='Male').
  384. Note that ``{% regroup %}`` does not order its input! Our example relies on
  385. the fact that the ``people`` list was ordered by ``gender`` in the first place.
  386. If the ``people`` list did *not* order its members by ``gender``, the regrouping
  387. would naively display more than one group for a single gender. For example,
  388. say the ``people`` list was set to this (note that the males are not grouped
  389. together):
  390. .. code-block:: python
  391. people = [
  392. {'first_name': 'Bill', 'last_name': 'Clinton', 'gender': 'Male'},
  393. {'first_name': 'Pat', 'last_name': 'Smith', 'gender': 'Unknown'},
  394. {'first_name': 'Margaret', 'last_name': 'Thatcher', 'gender': 'Female'},
  395. {'first_name': 'George', 'last_name': 'Bush', 'gender': 'Male'},
  396. {'first_name': 'Condoleezza', 'last_name': 'Rice', 'gender': 'Female'},
  397. ]
  398. With this input for ``people``, the example ``{% regroup %}`` template code
  399. above would result in the following output:
  400. * Male:
  401. * Bill Clinton
  402. * Unknown:
  403. * Pat Smith
  404. * Female:
  405. * Margaret Thatcher
  406. * Male:
  407. * George Bush
  408. * Female:
  409. * Condoleezza Rice
  410. The easiest solution to this gotcha is to make sure in your view code that the
  411. data is ordered according to how you want to display it.
  412. Another solution is to sort the data in the template using the ``dictsort``
  413. filter, if your data is in a list of dictionaries::
  414. {% regroup people|dictsort:"gender" by gender as gender_list %}
  415. .. templatetag:: spaceless
  416. spaceless
  417. ~~~~~~~~~
  418. Removes whitespace between HTML tags. This includes tab
  419. characters and newlines.
  420. Example usage::
  421. {% spaceless %}
  422. <p>
  423. <a href="foo/">Foo</a>
  424. </p>
  425. {% endspaceless %}
  426. This example would return this HTML::
  427. <p><a href="foo/">Foo</a></p>
  428. Only space between *tags* is removed -- not space between tags and text. In
  429. this example, the space around ``Hello`` won't be stripped::
  430. {% spaceless %}
  431. <strong>
  432. Hello
  433. </strong>
  434. {% endspaceless %}
  435. .. templatetag:: ssi
  436. ssi
  437. ~~~
  438. Output the contents of a given file into the page.
  439. Like a simple "include" tag, ``{% ssi %}`` includes the contents of another
  440. file -- which must be specified using an absolute path -- in the current
  441. page::
  442. {% ssi /home/html/ljworld.com/includes/right_generic.html %}
  443. If the optional "parsed" parameter is given, the contents of the included
  444. file are evaluated as template code, within the current context::
  445. {% ssi /home/html/ljworld.com/includes/right_generic.html parsed %}
  446. Note that if you use ``{% ssi %}``, you'll need to define
  447. :setting:`ALLOWED_INCLUDE_ROOTS` in your Django settings, as a security measure.
  448. See also: ``{% include %}``.
  449. .. templatetag:: templatetag
  450. templatetag
  451. ~~~~~~~~~~~
  452. Output one of the syntax characters used to compose template tags.
  453. Since the template system has no concept of "escaping", to display one of the
  454. bits used in template tags, you must use the ``{% templatetag %}`` tag.
  455. The argument tells which template bit to output:
  456. ================== =======
  457. Argument Outputs
  458. ================== =======
  459. ``openblock`` ``{%``
  460. ``closeblock`` ``%}``
  461. ``openvariable`` ``{{``
  462. ``closevariable`` ``}}``
  463. ``openbrace`` ``{``
  464. ``closebrace`` ``}``
  465. ``opencomment`` ``{#``
  466. ``closecomment`` ``#}``
  467. ================== =======
  468. .. templatetag:: url
  469. url
  470. ~~~
  471. Returns an absolute URL (i.e., a URL without the domain name) matching a given
  472. view function and optional parameters. This is a way to output links without
  473. violating the DRY principle by having to hard-code URLs in your templates::
  474. {% url path.to.some_view arg1,arg2,name1=value1 %}
  475. The first argument is a path to a view function in the format
  476. ``package.package.module.function``. Additional arguments are optional and
  477. should be comma-separated values that will be used as positional and keyword
  478. arguments in the URL. All arguments required by the URLconf should be present.
  479. For example, suppose you have a view, ``app_views.client``, whose URLconf
  480. takes a client ID (here, ``client()`` is a method inside the views file
  481. ``app_views.py``). The URLconf line might look like this:
  482. .. code-block:: python
  483. ('^client/(\d+)/$', 'app_views.client')
  484. If this app's URLconf is included into the project's URLconf under a path
  485. such as this:
  486. .. code-block:: python
  487. ('^clients/', include('project_name.app_name.urls'))
  488. ...then, in a template, you can create a link to this view like this::
  489. {% url app_views.client client.id %}
  490. The template tag will output the string ``/clients/client/123/``.
  491. .. versionadded:: 1.0
  492. If you're using :ref:`named URL patterns <naming-url-patterns>`, you can
  493. refer to the name of the pattern in the ``url`` tag instead of using the
  494. path to the view.
  495. Note that if the URL you're reversing doesn't exist, you'll get an
  496. :exc:`NoReverseMatch` exception raised, which will cause your site to display an
  497. error page.
  498. .. versionadded:: 1.0
  499. If you'd like to retrieve a URL without displaying it, you can use a slightly
  500. different call::
  501. {% url path.to.view arg, arg2 as the_url %}
  502. <a href="{{ the_url }}">I'm linking to {{ the_url }}</a>
  503. This ``{% url ... as var %}`` syntax will *not* cause an error if the view is
  504. missing. In practice you'll use this to link to views that are optional::
  505. {% url path.to.view as the_url %}
  506. {% if the_url %}
  507. <a href="{{ the_url }}">Link to optional stuff</a>
  508. {% endif %}
  509. .. templatetag:: widthratio
  510. widthratio
  511. ~~~~~~~~~~
  512. For creating bar charts and such, this tag calculates the ratio of a given value
  513. to a maximum value, and then applies that ratio to a constant.
  514. For example::
  515. <img src="bar.gif" height="10" width="{% widthratio this_value max_value 100 %}" />
  516. Above, if ``this_value`` is 175 and ``max_value`` is 200, the image in the
  517. above example will be 88 pixels wide (because 175/200 = .875; .875 * 100 = 87.5
  518. which is rounded up to 88).
  519. .. templatetag:: with
  520. with
  521. ~~~~
  522. .. versionadded:: 1.0
  523. Caches a complex variable under a simpler name. This is useful when accessing
  524. an "expensive" method (e.g., one that hits the database) multiple times.
  525. For example::
  526. {% with business.employees.count as total %}
  527. {{ total }} employee{{ total|pluralize }}
  528. {% endwith %}
  529. The populated variable (in the example above, ``total``) is only available
  530. between the ``{% with %}`` and ``{% endwith %}`` tags.
  531. .. _ref-templates-builtins-filters:
  532. Built-in filter reference
  533. -------------------------
  534. .. templatefilter:: add
  535. add
  536. ~~~
  537. Adds the argument to the value.
  538. For example::
  539. {{ value|add:"2" }}
  540. If ``value`` is ``4``, then the output will be ``6``.
  541. .. templatefilter:: addslashes
  542. addslashes
  543. ~~~~~~~~~~
  544. Adds slashes before quotes. Useful for escaping strings in CSV, for example.
  545. .. templatefilter:: capfirst
  546. capfirst
  547. ~~~~~~~~
  548. Capitalizes the first character of the value.
  549. .. templatefilter:: center
  550. center
  551. ~~~~~~
  552. Centers the value in a field of a given width.
  553. .. templatefilter:: cut
  554. cut
  555. ~~~
  556. Removes all values of arg from the given string.
  557. For example::
  558. {{ value|cut:" "}}
  559. If ``value`` is ``"String with spaces"``, the output will be ``"Stringwithspaces"``.
  560. .. templatefilter:: date
  561. date
  562. ~~~~
  563. Formats a date according to the given format (same as the `now`_ tag).
  564. For example::
  565. {{ value|date:"D d M Y" }}
  566. If ``value`` is a ``datetime`` object (e.g., the result of
  567. ``datetime.datetime.now()``), the output will be the string
  568. ``'Wed 09 Jan 2008'``.
  569. .. templatefilter:: default
  570. default
  571. ~~~~~~~
  572. If value evaluates to ``False``, use given default. Otherwise, use the value.
  573. For example::
  574. {{ value|default:"nothing" }}
  575. If ``value`` is ``""`` (the empty string), the output will be ``nothing``.
  576. .. templatefilter:: default_if_none
  577. default_if_none
  578. ~~~~~~~~~~~~~~~
  579. If (and only if) value is ``None``, use given default. Otherwise, use the
  580. value.
  581. Note that if an empty string is given, the default value will *not* be used.
  582. Use the ``default`` filter if you want to fallback for empty strings.
  583. For example::
  584. {{ value|default_if_none:"nothing" }}
  585. If ``value`` is ``None``, the output will be the string ``"nothing"``.
  586. .. templatefilter:: dictsort
  587. dictsort
  588. ~~~~~~~~
  589. Takes a list of dictionaries and returns that list sorted by the key given in
  590. the argument.
  591. For example::
  592. {{ value|dictsort:"name" }}
  593. If ``value`` is:
  594. .. code-block:: python
  595. [
  596. {'name': 'zed', 'age': 19},
  597. {'name': 'amy', 'age': 22},
  598. {'name': 'joe', 'age': 31},
  599. ]
  600. then the output would be:
  601. .. code-block:: python
  602. [
  603. {'name': 'amy', 'age': 22},
  604. {'name': 'joe', 'age': 31},
  605. {'name': 'zed', 'age': 19},
  606. ]
  607. .. templatefilter:: dictsortreversed
  608. dictsortreversed
  609. ~~~~~~~~~~~~~~~~
  610. Takes a list of dictionaries and returns that list sorted in reverse order by
  611. the key given in the argument. This works exactly the same as the above filter,
  612. but the returned value will be in reverse order.
  613. .. templatefilter:: divisibleby
  614. divisibleby
  615. ~~~~~~~~~~~
  616. Returns ``True`` if the value is divisible by the argument.
  617. For example::
  618. {{ value|divisibleby:"3" }}
  619. If ``value`` is ``21``, the output would be ``True``.
  620. .. templatefilter:: escape
  621. escape
  622. ~~~~~~
  623. Escapes a string's HTML. Specifically, it makes these replacements:
  624. * ``<`` is converted to ``&lt;``
  625. * ``>`` is converted to ``&gt;``
  626. * ``'`` (single quote) is converted to ``&#39;``
  627. * ``"`` (double quote) is converted to ``&quot;``
  628. * ``&`` is converted to ``&amp;``
  629. The escaping is only applied when the string is output, so it does not matter
  630. where in a chained sequence of filters you put ``escape``: it will always be
  631. applied as though it were the last filter. If you want escaping to be applied
  632. immediately, use the ``force_escape`` filter.
  633. Applying ``escape`` to a variable that would normally have auto-escaping
  634. applied to the result will only result in one round of escaping being done. So
  635. it is safe to use this function even in auto-escaping environments. If you want
  636. multiple escaping passes to be applied, use the ``force_escape`` filter.
  637. .. versionchanged:: 1.0
  638. Due to auto-escaping, the behavior of this filter has changed slightly.
  639. The replacements are only made once, after
  640. all other filters are applied -- including filters before and after it.
  641. .. templatefilter:: escapejs
  642. escapejs
  643. ~~~~~~~~
  644. .. versionadded:: 1.0
  645. Escapes characters for use in JavaScript strings. This does *not* make the
  646. string safe for use in HTML, but does protect you from syntax errors when using
  647. templates to generate JavaScript/JSON.
  648. .. templatefilter:: filesizeformat
  649. filesizeformat
  650. ~~~~~~~~~~~~~~
  651. Format the value like a 'human-readable' file size (i.e. ``'13 KB'``,
  652. ``'4.1 MB'``, ``'102 bytes'``, etc).
  653. For example::
  654. {{ value|filesizeformat }}
  655. If ``value`` is 123456789, the output would be ``117.7 MB``.
  656. .. templatefilter:: first
  657. first
  658. ~~~~~
  659. Returns the first item in a list.
  660. For example::
  661. {{ value|first }}
  662. If ``value`` is the list ``['a', 'b', 'c']``, the output will be ``'a'``.
  663. .. templatefilter:: fix_ampersands
  664. fix_ampersands
  665. ~~~~~~~~~~~~~~
  666. .. versionchanged:: 1.0
  667. This is rarely useful as ampersands are now automatically escaped. See escape_ for more information.
  668. Replaces ampersands with ``&amp;`` entities.
  669. For example::
  670. {{ value|fix_ampersands }}
  671. If ``value`` is ``Tom & Jerry``, the output will be ``Tom &amp; Jerry``.
  672. .. templatefilter:: floatformat
  673. floatformat
  674. ~~~~~~~~~~~
  675. When used without an argument, rounds a floating-point number to one decimal
  676. place -- but only if there's a decimal part to be displayed. For example:
  677. ============ =========================== ========
  678. ``value`` Template Output
  679. ============ =========================== ========
  680. ``34.23234`` ``{{ value|floatformat }}`` ``34.2``
  681. ``34.00000`` ``{{ value|floatformat }}`` ``34``
  682. ``34.26000`` ``{{ value|floatformat }}`` ``34.3``
  683. ============ =========================== ========
  684. If used with a numeric integer argument, ``floatformat`` rounds a number to
  685. that many decimal places. For example:
  686. ============ ============================= ==========
  687. ``value`` Template Output
  688. ============ ============================= ==========
  689. ``34.23234`` ``{{ value|floatformat:3 }}`` ``34.232``
  690. ``34.00000`` ``{{ value|floatformat:3 }}`` ``34.000``
  691. ``34.26000`` ``{{ value|floatformat:3 }}`` ``34.260``
  692. ============ ============================= ==========
  693. If the argument passed to ``floatformat`` is negative, it will round a number
  694. to that many decimal places -- but only if there's a decimal part to be
  695. displayed. For example:
  696. ============ ================================ ==========
  697. ``value`` Template Output
  698. ============ ================================ ==========
  699. ``34.23234`` ``{{ value|floatformat:"-3" }}`` ``34.232``
  700. ``34.00000`` ``{{ value|floatformat:"-3" }}`` ``34``
  701. ``34.26000`` ``{{ value|floatformat:"-3" }}`` ``34.260``
  702. ============ ================================ ==========
  703. Using ``floatformat`` with no argument is equivalent to using ``floatformat``
  704. with an argument of ``-1``.
  705. .. templatefilter:: force_escape
  706. force_escape
  707. ~~~~~~~~~~~~
  708. .. versionadded:: 1.0
  709. Applies HTML escaping to a string (see the ``escape`` filter for details).
  710. This filter is applied *immediately* and returns a new, escaped string. This
  711. is useful in the rare cases where you need multiple escaping or want to apply
  712. other filters to the escaped results. Normally, you want to use the ``escape``
  713. filter.
  714. .. templatefilter:: get_digit
  715. get_digit
  716. ~~~~~~~~~
  717. Given a whole number, returns the requested digit, where 1 is the right-most
  718. digit, 2 is the second-right-most digit, etc. Returns the original value for
  719. invalid input (if input or argument is not an integer, or if argument is less
  720. than 1). Otherwise, output is always an integer.
  721. For example::
  722. {{ value|get_digit:"2" }}
  723. If ``value`` is ``123456789``, the output will be ``8``.
  724. .. templatefilter:: iriendcode
  725. iriencode
  726. ~~~~~~~~~
  727. Converts an IRI (Internationalized Resource Identifier) to a string that is
  728. suitable for including in a URL. This is necessary if you're trying to use
  729. strings containing non-ASCII characters in a URL.
  730. It's safe to use this filter on a string that has already gone through the
  731. ``urlencode`` filter.
  732. .. templatefilter:: join
  733. join
  734. ~~~~
  735. Joins a list with a string, like Python's ``str.join(list)``
  736. For example::
  737. {{ value|join:" // " }}
  738. If ``value`` is the list ``['a', 'b', 'c']``, the output will be the string
  739. ``"a // b // c"``.
  740. .. templatefilter:: last
  741. last
  742. ~~~~
  743. .. versionadded:: 1.0
  744. Returns the last item in a list.
  745. For example::
  746. {{ value|last }}
  747. If ``value`` is the list ``['a', 'b', 'c', 'd']``, the output will be the string
  748. ``"d"``.
  749. .. templatefilter:: length
  750. length
  751. ~~~~~~
  752. Returns the length of the value. This works for both strings and lists.
  753. For example::
  754. {{ value|length }}
  755. If ``value`` is ``['a', 'b', 'c', 'd']``, the output will be ``4``.
  756. .. templatefilter:: length_is
  757. length_is
  758. ~~~~~~~~~
  759. Returns ``True`` if the value's length is the argument, or ``False`` otherwise.
  760. For example::
  761. {{ value|length_is:"4" }}
  762. If ``value`` is ``['a', 'b', 'c', 'd']``, the output will be ``True``.
  763. .. templatefilter:: linebreaks
  764. linebreaks
  765. ~~~~~~~~~~
  766. Replaces line breaks in plain text with appropriate HTML; a single
  767. newline becomes an HTML line break (``<br />``) and a new line
  768. followed by a blank line becomes a paragraph break (``</p>``).
  769. For example::
  770. {{ value|linebreaks }}
  771. If ``value`` is ``Joel\nis a slug``, the output will be ``<p>Joel<br>is a
  772. slug</p>``.
  773. .. templatefilter:: linebreaksbr
  774. linebreaksbr
  775. ~~~~~~~~~~~~
  776. Converts all newlines in a piece of plain text to HTML line breaks
  777. (``<br />``).
  778. .. templatefilter:: linenumbers
  779. linenumbers
  780. ~~~~~~~~~~~
  781. Displays text with line numbers.
  782. .. templatefilter:: ljust
  783. ljust
  784. ~~~~~
  785. Left-aligns the value in a field of a given width.
  786. **Argument:** field size
  787. .. templatefilter:: lower
  788. lower
  789. ~~~~~
  790. Converts a string into all lowercase.
  791. For example::
  792. {{ value|lower }}
  793. If ``value`` is ``Still MAD At Yoko``, the output will be ``still mad at yoko``.
  794. .. templatefilter:: make_list
  795. make_list
  796. ~~~~~~~~~
  797. Returns the value turned into a list. For an integer, it's a list of
  798. digits. For a string, it's a list of characters.
  799. For example::
  800. {{ value|make_list }}
  801. If ``value`` is the string ``"Joel"``, the output would be the list
  802. ``[u'J', u'o', u'e', u'l']``. If ``value`` is ``123``, the output will be the
  803. list ``[1, 2, 3]``.
  804. .. templatefilter:: phone2numeric
  805. phone2numeric
  806. ~~~~~~~~~~~~~
  807. Converts a phone number (possibly containing letters) to its numerical
  808. equivalent. For example, ``'800-COLLECT'`` will be converted to
  809. ``'800-2655328'``.
  810. The input doesn't have to be a valid phone number. This will happily convert
  811. any string.
  812. .. templatefilter:: pluralize
  813. pluralize
  814. ~~~~~~~~~
  815. Returns a plural suffix if the value is not 1. By default, this suffix is ``'s'``.
  816. Example::
  817. You have {{ num_messages }} message{{ num_messages|pluralize }}.
  818. For words that require a suffix other than ``'s'``, you can provide an alternate
  819. suffix as a parameter to the filter.
  820. Example::
  821. You have {{ num_walruses }} walrus{{ num_walrus|pluralize:"es" }}.
  822. For words that don't pluralize by simple suffix, you can specify both a
  823. singular and plural suffix, separated by a comma.
  824. Example::
  825. You have {{ num_cherries }} cherr{{ num_cherries|pluralize:"y,ies" }}.
  826. .. templatefilter:: pprint
  827. pprint
  828. ~~~~~~
  829. A wrapper around `pprint.pprint`__ -- for debugging, really.
  830. __ http://www.python.org/doc/2.5/lib/module-pprint.html
  831. .. templatefilter:: random
  832. random
  833. ~~~~~~
  834. Returns a random item from the given list.
  835. For example::
  836. {{ value|random }}
  837. If ``value`` is the list ``['a', 'b', 'c', 'd']``, the output could be ``"b"``.
  838. .. templatefilter:: removetags
  839. removetags
  840. ~~~~~~~~~~
  841. Removes a space-separated list of [X]HTML tags from the output.
  842. For example::
  843. {{ value|removetags:"b span"|safe }}
  844. If ``value`` is ``"<b>Joel</b> <button>is</button> a <span>slug</span>"`` the
  845. output will be ``"Joel <button>is</button> a slug"``.
  846. .. templatefilter:: rjust
  847. rjust
  848. ~~~~~
  849. Right-aligns the value in a field of a given width.
  850. **Argument:** field size
  851. .. templatefilter:: safe
  852. safe
  853. ~~~~
  854. Marks a string as not requiring further HTML escaping prior to output. When
  855. autoescaping is off, this filter has no effect.
  856. .. templatefilter:: slice
  857. slice
  858. ~~~~~
  859. Returns a slice of the list.
  860. Uses the same syntax as Python's list slicing. See
  861. http://diveintopython.org/native_data_types/lists.html#odbchelper.list.slice
  862. for an introduction.
  863. Example::
  864. {{ some_list|slice:":2" }}
  865. .. templatefilter:: slugify
  866. slugify
  867. ~~~~~~~
  868. Converts to lowercase, removes non-word characters (alphanumerics and
  869. underscores) and converts spaces to hyphens. Also strips leading and trailing
  870. whitespace.
  871. For example::
  872. {{ value|slugify }}
  873. If ``value`` is ``"Joel is a slug"``, the output will be ``"joel-is-a-slug"``.
  874. .. templatefilter:: stringformat
  875. stringformat
  876. ~~~~~~~~~~~~
  877. Formats the variable according to the argument, a string formatting specifier.
  878. This specifier uses Python string formatting syntax, with the exception that
  879. the leading "%" is dropped.
  880. See http://docs.python.org/lib/typesseq-strings.html for documentation of
  881. Python string formatting
  882. For example::
  883. {{ value|stringformat:"s" }}
  884. If ``value`` is ``"Joel is a slug"``, the output will be ``"Joel is a slug"``.
  885. .. templatefilter:: striptags
  886. striptags
  887. ~~~~~~~~~
  888. Strips all [X]HTML tags.
  889. For example::
  890. {{ value|striptags }}
  891. If ``value`` is ``"<b>Joel</b> <button>is</button> a <span>slug</span>"``, the
  892. output will be ``"Joel is a slug"``.
  893. .. templatefilter:: time
  894. time
  895. ~~~~
  896. Formats a time according to the given format (same as the `now`_ tag).
  897. The time filter will only accept parameters in the format string that relate
  898. to the time of day, not the date (for obvious reasons). If you need to
  899. format a date, use the `date`_ filter.
  900. For example::
  901. {{ value|time:"H:i" }}
  902. If ``value`` is equivalent to ``datetime.datetime.now()``, the output will be
  903. the string ``"01:23"``.
  904. .. templatefilter:: timesince
  905. timesince
  906. ~~~~~~~~~
  907. Formats a date as the time since that date (e.g., "4 days, 6 hours").
  908. Takes an optional argument that is a variable containing the date to use as
  909. the comparison point (without the argument, the comparison point is *now*).
  910. For example, if ``blog_date`` is a date instance representing midnight on 1
  911. June 2006, and ``comment_date`` is a date instance for 08:00 on 1 June 2006,
  912. then ``{{ blog_date|timesince:comment_date }}`` would return "8 hours".
  913. Comparing offset-naive and offset-aware datetimes will return an empty string.
  914. Minutes is the smallest unit used, and "0 minutes" will be returned for any
  915. date that is in the future relative to the comparison point.
  916. .. templatefilter:: timeuntil
  917. timeuntil
  918. ~~~~~~~~~
  919. Similar to ``timesince``, except that it measures the time from now until the
  920. given date or datetime. For example, if today is 1 June 2006 and
  921. ``conference_date`` is a date instance holding 29 June 2006, then
  922. ``{{ conference_date|timeuntil }}`` will return "4 weeks".
  923. Takes an optional argument that is a variable containing the date to use as
  924. the comparison point (instead of *now*). If ``from_date`` contains 22 June
  925. 2006, then ``{{ conference_date|timeuntil:from_date }}`` will return "1 week".
  926. Comparing offset-naive and offset-aware datetimes will return an empty string.
  927. Minutes is the smallest unit used, and "0 minutes" will be returned for any
  928. date that is in the past relative to the comparison point.
  929. .. templatefilter:: title
  930. title
  931. ~~~~~
  932. Converts a string into titlecase.
  933. .. templatefilter:: truncatewords
  934. truncatewords
  935. ~~~~~~~~~~~~~
  936. Truncates a string after a certain number of words.
  937. **Argument:** Number of words to truncate after
  938. For example::
  939. {{ value|truncatewords:2 }}
  940. If ``value`` is ``"Joel is a slug"``, the output will be ``"Joel is ..."``.
  941. .. templatefilter:: truncatewords_html
  942. truncatewords_html
  943. ~~~~~~~~~~~~~~~~~~
  944. Similar to ``truncatewords``, except that it is aware of HTML tags. Any tags
  945. that are opened in the string and not closed before the truncation point, are
  946. closed immediately after the truncation.
  947. This is less efficient than ``truncatewords``, so should only be used when it
  948. is being passed HTML text.
  949. .. templatefilter:: unordered_list
  950. unordered_list
  951. ~~~~~~~~~~~~~~
  952. Recursively takes a self-nested list and returns an HTML unordered list --
  953. WITHOUT opening and closing <ul> tags.
  954. .. versionchanged:: 1.0
  955. The format accepted by ``unordered_list`` has changed to be easier to understand.
  956. The list is assumed to be in the proper format. For example, if ``var`` contains
  957. ``['States', ['Kansas', ['Lawrence', 'Topeka'], 'Illinois']]``, then
  958. ``{{ var|unordered_list }}`` would return::
  959. <li>States
  960. <ul>
  961. <li>Kansas
  962. <ul>
  963. <li>Lawrence</li>
  964. <li>Topeka</li>
  965. </ul>
  966. </li>
  967. <li>Illinois</li>
  968. </ul>
  969. </li>
  970. Note: the previous more restrictive and verbose format is still supported:
  971. ``['States', [['Kansas', [['Lawrence', []], ['Topeka', []]]], ['Illinois', []]]]``,
  972. .. templatefilter:: upper
  973. upper
  974. ~~~~~
  975. Converts a string into all uppercase.
  976. For example::
  977. {{ value|upper }}
  978. If ``value`` is ``"Joel is a slug"``, the output will be ``"JOEL IS A SLUG"``.
  979. .. templatefilter:: urlencode
  980. urlencode
  981. ~~~~~~~~~
  982. Escapes a value for use in a URL.
  983. .. templatefilter:: urlize
  984. urlize
  985. ~~~~~~
  986. Converts URLs in plain text into clickable links.
  987. Note that if ``urlize`` is applied to text that already contains HTML markup,
  988. things won't work as expected. Apply this filter only to *plain* text.
  989. For example::
  990. {{ value|urlize }}
  991. If ``value`` is ``"Check out www.djangoproject.com"``, the output will be
  992. ``"Check out <a
  993. href="http://www.djangoproject.com">www.djangoproject.com</a>"``.
  994. .. templatefilter:: urlizetrunc
  995. urlizetrunc
  996. ~~~~~~~~~~~
  997. Converts URLs into clickable links, truncating URLs longer than the given
  998. character limit.
  999. As with urlize_, this filter should only be applied to *plain* text.
  1000. **Argument:** Length to truncate URLs to
  1001. For example::
  1002. {{ value|urlizetrunc:15 }}
  1003. If ``value`` is ``"Check out www.djangoproject.com"``, the output would be
  1004. ``'Check out <a
  1005. href="http://www.djangoproject.com">www.djangopr...</a>'``.
  1006. .. templatefilter:: wordcount
  1007. wordcount
  1008. ~~~~~~~~~
  1009. Returns the number of words.
  1010. .. templatefilter:: wordwrap
  1011. wordwrap
  1012. ~~~~~~~~
  1013. Wraps words at specified line length.
  1014. **Argument:** number of characters at which to wrap the text
  1015. For example::
  1016. {{ value|wordwrap:5 }}
  1017. If ``value`` is ``Joel is a slug``, the output would be::
  1018. Joel
  1019. is a
  1020. slug
  1021. .. templatefilter:: yesno
  1022. yesno
  1023. ~~~~~
  1024. Given a string mapping values for true, false and (optionally) None,
  1025. returns one of those strings according to the value:
  1026. ========== ====================== ==================================
  1027. Value Argument Outputs
  1028. ========== ====================== ==================================
  1029. ``True`` ``"yeah,no,maybe"`` ``yeah``
  1030. ``False`` ``"yeah,no,maybe"`` ``no``
  1031. ``None`` ``"yeah,no,maybe"`` ``maybe``
  1032. ``None`` ``"yeah,no"`` ``"no"`` (converts None to False
  1033. if no mapping for None is given)
  1034. ========== ====================== ==================================
  1035. Other tags and filter libraries
  1036. ===============================
  1037. Django comes with a couple of other template-tag libraries that you have to
  1038. enable explicitly in your ``INSTALLED_APPS`` setting and enable in your
  1039. template with the ``{% load %}`` tag.
  1040. django.contrib.humanize
  1041. -----------------------
  1042. A set of Django template filters useful for adding a "human touch" to data. See
  1043. :ref:`ref-contrib-humanize`.
  1044. django.contrib.markup
  1045. ---------------------
  1046. A collection of template filters that implement these common markup languages:
  1047. * Textile
  1048. * Markdown
  1049. * ReST (ReStructured Text)
  1050. See :ref:`ref-contrib-markup`.
  1051. django.contrib.webdesign
  1052. ------------------------
  1053. A collection of template tags that can be useful while designing a website,
  1054. such as a generator of Lorem Ipsum text. See :ref:`ref-contrib-webdesign`.