笨鸟编程-零基础入门Pyhton教程

 找回密码
 立即注册

模板设计者文档

发布者: 笨鸟自学网



dictsort(valuecase_sensitive=Falseby='key')

Sort a dict and yield (key, value) pairs. Because python dicts are unsorted you may want to use this function to order them by either key or value:

{% for item in mydict|dictsort %}
    sort the dict by key, case insensitive

{% for item in mydict|dictsort(true) %}
    sort the dict by key, case sensitive

{% for item in mydict|dictsort(false, 'value') %}
    sort the dict by value, case insensitive
escape(s)

Convert the characters &, <, >, ‘, and ” in string s to HTML-safe sequences. Use this if you need to display text that might contain such characters in HTML. Marks return value as markup string.

Aliases:e
filesizeformat(valuebinary=False)

Format the value like a ‘human-readable’ file size (i.e. 13 kB, 4.1 MB, 102 Bytes, etc). Per default decimal prefixes are used (Mega, Giga, etc.), if the second parameter is set to True the binary prefixes are used (Mebi, Gibi).

first(seq)

Return the first item of a sequence.

float(valuedefault=0.0)

Convert the value into a floating point number. If the conversion doesn’t work it will return 0.0. You can override this default using the first parameter.

forceescape(value)

Enforce HTML escaping. This will probably double escape variables.

format(value*args**kwargs)

Apply python string formatting on an object:

{{ "%s - %s"|format("Hello?", "Foo!") }}
    -> Hello? - Foo!
groupby(valueattribute)

Group a sequence of objects by a common attribute.

If you for example have a list of dicts or objects that represent persons with genderfirst_name and last_name attributes and you want to group all users by genders you can do something like the following snippet:

<ul>
{% for group in persons|groupby('gender') %}
    <li>{{ group.grouper }}<ul>
    {% for person in group.list %}
        <li>{{ person.first_name }} {{ person.last_name }}</li>
    {% endfor %}</ul></li>
{% endfor %}
</ul>

Additionally it’s possible to use tuple unpacking for the grouper and list:

<ul>
{% for grouper, list in persons|groupby('gender') %}
    ...
{% endfor %}
</ul>

As you can see the item we’re grouping by is stored in the grouper attribute and the list contains all the objects that have this grouper in common.

Changed in version 2.6: It’s now possible to use dotted notation to group by the child attribute of another attribute.

indent(swidth=4indentfirst=False)

Return a copy of the passed string, each line indented by 4 spaces. The first line is not indented. If you want to change the number of spaces or indent the first line too you can pass additional parameters to the filter:

{{ mytext|indent(2, true) }}
    indent by two spaces and indent the first line too.
int(valuedefault=0base=10)

Convert the value into an integer. If the conversion doesn’t work it will return 0. You can override this default using the first parameter. You can also override the default base (10) in the second parameter, which handles input with prefixes such as 0b, 0o and 0x for bases 2, 8 and 16 respectively.

join(valued=u''attribute=None)

Return a string which is the concatenation of the strings in the sequence. The separator between elements is an empty string per default, you can define it with the optional parameter:

{{ [1, 2, 3]|join('|') }}
    -> 1|2|3

{{ [1, 2, 3]|join }}
    -> 123

It is also possible to join certain attributes of an object:

{{ users|join(', ', attribute='username') }}

New in version 2.6: The attribute parameter was added.

last(seq)

Return the last item of a sequence.

length(object)

Return the number of items of a sequence or mapping.

Aliases:count
list(value)

Convert the value into a list. If it was a string the returned list will be a list of characters.

lower(s)

Convert a value to lowercase.

map()

Applies a filter on a sequence of objects or looks up an attribute. This is useful when dealing with lists of objects but you are really only interested in a certain value of it.

The basic usage is mapping on an attribute. Imagine you have a list of users but you are only interested in a list of usernames:

Users on this page: {{ users|map(attribute='username')|join(', ') }}

Alternatively you can let it invoke a filter by passing the name of the filter and the arguments afterwards. A good example would be applying a text conversion filter on a sequence:

Users on this page: {{ titles|map('lower')|join(', ') }}

New in version 2.7.


上一篇:沙箱下一篇:扩展

Archiver|手机版|笨鸟自学网 ( 粤ICP备20019910号 )

GMT+8, 2024-9-17 04:41 , Processed in 0.041521 second(s), 17 queries .

© 2001-2020

返回顶部