找回密码
 立即注册

模板设计者文档

2022-2-22 23:49| 发布者: 笨鸟自学网| 查看: 12089| 评论: 0

摘要: 这份文档描述了模板引擎中的语法和语义结构,对于创建 Jinja 模板是一份相当有用 的参考。因为模板引擎非常灵活,应用中的配置会在分隔符和未定义值的行为方面与 这里的配置有细微差异。概要模板仅仅是文本文件。它 ...


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, 2025-9-16 20:05 , Processed in 0.015610 second(s), 18 queries .

© 2001-2020

返回顶部