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

 找回密码
 立即注册

模板设计者文档

发布者: 笨鸟自学网



pprint(valueverbose=False)

Pretty print a variable. Useful for debugging.

With Jinja 1.2 onwards you can pass it a parameter. If this parameter is truthy the output will be more verbose (this requires pretty)

random(seq)

Return a random item from the sequence.

reject()

Filters a sequence of objects by applying a test to the object and rejecting the ones with the test succeeding.

Example usage:

{{ numbers|reject("odd") }}

New in version 2.7.

rejectattr()

Filters a sequence of objects by applying a test to an attribute of an object or the attribute and rejecting the ones with the test succeeding.

{{ users|rejectattr("is_active") }}
{{ users|rejectattr("email", "none") }}

New in version 2.7.

replace(soldnewcount=None)

Return a copy of the value with all occurrences of a substring replaced with a new one. The first argument is the substring that should be replaced, the second is the replacement string. If the optional third argument count is given, only the first count occurrences are replaced:

{{ "Hello World"|replace("Hello", "Goodbye") }}
    -> Goodbye World

{{ "aaaaargh"|replace("a", "d'oh, ", 2) }}
    -> d'oh, d'oh, aaargh
reverse(value)

Reverse the object or return an iterator that iterates over it the other way round.

round(valueprecision=0method='common')

Round the number to a given precision. The first parameter specifies the precision (default is 0), the second the rounding method:

  • 'common' rounds either up or down
  • 'ceil' always rounds up
  • 'floor' always rounds down

If you don’t specify a method 'common' is used.

{{ 42.55|round }}
    -> 43.0
{{ 42.55|round(1, 'floor') }}
    -> 42.5

Note that even if rounded to 0 precision, a float is returned. If you need a real integer, pipe it through int:

{{ 42.55|round|int }}
    -> 43
safe(value)

Mark the value as safe which means that in an environment with automatic escaping enabled this variable will not be escaped.

select()

Filters a sequence of objects by applying a test to the object and only selecting the ones with the test succeeding.

Example usage:

{{ numbers|select("odd") }}
{{ numbers|select("odd") }}

New in version 2.7.

selectattr()

Filters a sequence of objects by applying a test to an attribute of an object and only selecting the ones with the test succeeding.

Example usage:

{{ users|selectattr("is_active") }}
{{ users|selectattr("email", "none") }}

New in version 2.7.

slice(valueslicesfill_with=None)

Slice an iterator and return a list of lists containing those items. Useful if you want to create a div containing three ul tags that represent columns:

<div class="columwrapper">
  {%- for column in items|slice(3) %}
    <ul class="column-{{ loop.index }}">
    {%- for item in column %}
      <li>{{ item }}</li>
    {%- endfor %}
    </ul>
  {%- endfor %}
</div>

If you pass it a second argument it’s used to fill missing values on the last iteration.

sort(valuereverse=Falsecase_sensitive=Falseattribute=None)

Sort an iterable. Per default it sorts ascending, if you pass it true as first argument it will reverse the sorting.

If the iterable is made of strings the third parameter can be used to control the case sensitiveness of the comparison which is disabled by default.

{% for item in iterable|sort %}
    ...
{% endfor %}

It is also possible to sort by an attribute (for example to sort by the date of an object) by specifying the attribute parameter:

{% for item in iterable|sort(attribute='date') %}
    ...
{% endfor %}

Changed in version 2.6: The attribute parameter was added.

string(object)

Make a string unicode if it isn’t already. That way a markup string is not converted back to unicode.

striptags(value)

Strip SGML/XML tags and replace adjacent whitespace by one space.


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

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

GMT+8, 2024-12-22 13:20 , Processed in 0.025651 second(s), 17 queries .

© 2001-2020

返回顶部