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

 找回密码
 立即注册

模板设计者文档

发布者: 笨鸟自学网



sum(iterableattribute=Nonestart=0)

Returns the sum of a sequence of numbers plus the value of parameter ‘start’ (which defaults to 0). When the sequence is empty it returns start.

It is also possible to sum up only certain attributes:

Total: {{ items|sum(attribute='price') }}

Changed in version 2.6: The attribute parameter was added to allow suming up over attributes. Also the start parameter was moved on to the right.

title(s)

Return a titlecased version of the value. I.e. words will start with uppercase letters, all remaining characters are lowercase.

trim(value)

Strip leading and trailing whitespace.

truncate(slength=255killwords=Falseend='...')

Return a truncated copy of the string. The length is specified with the first parameter which defaults to 255. If the second parameter is true the filter will cut the text at length. Otherwise it will discard the last word. If the text was in fact truncated it will append an ellipsis sign ("..."). If you want a different ellipsis sign than "..." you can specify it using the third parameter.

{{ "foo bar baz"|truncate(9) }}
    -> "foo ..."
{{ "foo bar baz"|truncate(9, True) }}
    -> "foo ba..."
upper(s)

Convert a value to uppercase.

urlencode(value)

Escape strings for use in URLs (uses UTF-8 encoding). It accepts both dictionaries and regular strings as well as pairwise iterables.

New in version 2.7.

urlize(valuetrim_url_limit=Nonenofollow=Falsetarget=None)

Converts URLs in plain text into clickable links.

If you pass the filter an additional integer it will shorten the urls to that number. Also a third argument exists that makes the urls “nofollow”:

{{ mytext|urlize(40, true) }}
    links are shortened to 40 chars and defined with rel="nofollow"

If target is specified, the target attribute will be added to the <a> tag:

{{ mytext|urlize(40, target='_blank') }}

Changed in version 2.8+: The target parameter was added.

wordcount(s)

Count the words in that string.

wordwrap(swidth=79break_long_words=Truewrapstring=None)

Return a copy of the string passed to the filter wrapped after 79 characters. You can override this default using the first parameter. If you set the second parameter to false Jinja will not split words apart if they are longer than width. By default, the newlines will be the default newlines for the environment, but this can be changed using the wrapstring keyword argument.

New in version 2.7: Added support for the wrapstring parameter.

xmlattr(dautospace=True)

Create an SGML/XML attribute string based on the items in a dict. All values that are neither none nor undefined are automatically escaped:

<ul{{ {'class': 'my_list', 'missing': none,
        'id': 'list-%d'|format(variable)}|xmlattr }}>
...
</ul>

Results in something like this:

<ul class="my_list" id="list-42">
...
</ul>

As you can see it automatically prepends a space in front of the item if the filter returned something unless the second parameter is false.


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

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

GMT+8, 2024-9-19 09:40 , Processed in 0.292573 second(s), 33 queries .

© 2001-2020

返回顶部