Code

Opened 6 years ago

Closed 6 years ago

#6351 closed (fixed)

Template documentation could do with a little more example code

Reported by: atlithorn <atlithorn@…> Owned by: nobody
Component: Documentation Version: master
Severity: Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

A lot of the filters in http://www.djangoproject.com/documentation/templates/ could do with a simple example. Here are examples for most of those missing

value template output
4 {{value:add:2}} 6
String with spaces {{value|cut:" "}} Stringwithspaces
datetime.datetime.now() {{value|date:"D d M Y"}} Wed 09 Jan 2008
Undefined {{value|default:"nothing"}} nothing
None {{value|default_if_none:"nothing"}} nothing
[{'n':'g','m':0},{'n':'a','m':2},{'n':'z','m':8}] {{value|dictsort:"n"}} [{'n':'a','m':2},{'n':'g','m':0},{'n':'z','m':8}]
[{'n':'g','m':0},{'n':'a','m':2},{'n':'z','m':8}] {{value|dictsortreversed:"n"}} [{'n':'z','m':8},{'n':'g','m':0},{'n':'a','m':2},]
123456789 {{value|divisibleby:3}} True
123456789 {{value|filesizeformat}} 117.7 MB
['a','b','c'] {{value|first}} a
123456789 {{value|get_digit:2}} 8
['a','b','c','d'] {{value|join:" or "}} a or b or c or d
['a','b','c','d'] {{value|last}} d
['a','b','c','d'] {{value|length}} 4
['a','b','c','d'] {{value|length_is:4}} True
Joel\nis a slug {{value|linebreaks}} <p>Joel<br>is a slug</p>
Joel is a slug {{value|lower}} joel is a slug
123456789 {{value|make_list}} [u'1', u'2', u'3', u'4', u'5', u'6', u'7', u'8', u'9']
Joel is a slug {{value|make_list}} [u'J', u'o', u'e', u'l', u' ', u'i', u's', u' ', u'a', u' ', u's', u'l', u'u', u'g']
['a','b','c','d'] {{value|random}} b
<b>Joel</b> <button>is</button> a <span>slug</span> {{value|removetags: "b span"|safe}} Joel <button>is</button> a slug
['a','b','c','d'] {{value|slice:":2"}} ['a','b']
Joel is a slug {{value|slugify}} joel-is-a-slug
Joel is a slug {{value|stringformat:"s"}} Joel is a slug
<b>Joel</b> <button>is</button> a <span>slug</span> {{value|striptags}} Joel is a slug
datetime.datetime.now() {{value|time:"H:i"}} 01:23
Joel is a slug {{value|truncatewords:2}} Joel is ...
Joel is a slug {{value|upper}} JOEL IS A SLUG
Joel is a slug {{value|wordcount}} 4

And some more:

Check out www.djangoproject.com -> {{value|urlize}} -> Check out <a href="http://www.djangoproject.com">www.djangoproject.com</a>
Check out www.djangoproject.com -> {{value|urlizetrunc:15}} -> Check out <a href="http://www.djangoproject.com">www.djangopr...</a> 
Joel is a slug -> {{value|wordwrap:5}} -> Joel 
is a 
slug

Attachments (1)

filter_doc.diff (8.2 KB) - added by dtulig 6 years ago.
Turned ticket description into a patch

Download all attachments as: .zip

Change History (6)

comment:1 Changed 6 years ago by atlithorn <atlithorn@…>

  • Component changed from Uncategorized to Documentation
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

Changed 6 years ago by dtulig

Turned ticket description into a patch

comment:2 Changed 6 years ago by dtulig

  • Has patch set

comment:3 Changed 6 years ago by Simon Greenhill <dev@…>

  • Triage Stage changed from Unreviewed to Ready for checkin

Joel is a slug?

comment:4 Changed 6 years ago by atlithorn <atlithorn@…>

:) 'String with spaces' was just too boring. Joel's sluggish nature was the first output of the random string generator in my head. Feel free to ctrl-h.

comment:5 Changed 6 years ago by jacob

  • Resolution set to fixed
  • Status changed from new to closed

(In [7276]) Fixed #6351: added more examples to template filters. Thanks, David Tulig and atlithorn.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.