flask(11)过滤器

字符串操作:
safe:禁用转义;

  <p>{{ '<em>hello</em>' | safe }}</p>

capitalize:把变量值的首字母转成大写,其余字母转小写;

  <p>{{ 'hello' | capitalize }}</p>

lower:把值转成小写;

  <p>{{ 'HELLO' | lower }}</p>

upper:把值转成大写;

  <p>{{ 'hello' | upper }}</p>

title:把值中的每个单词的首字母都转成大写;

  <p>{{ 'hello' | title }}</p>

trim:把值的首尾空格去掉;

  <p>{{ ' hello world ' | trim }}</p>

reverse:字符串反转;

  <p>{{ 'olleh' | reverse }}</p>

format:格式化输出;

  <p>{{ '%s is %d' | format('name',17) }}</p>

striptags:渲染之前把值中所有的HTML标签都删掉;

  <p>{{ '<em>hello</em>' | striptags }}</p>

列表操作
first:取第一个元素

  <p>{{ [1,2,3,4,5,6] | first }}</p>

last:取最后一个元素

  <p>{{ [1,2,3,4,5,6] | last }}</p>

length:获取列表长度

  <p>{{ [1,2,3,4,5,6] | length }}</p>

sum:列表求和

  <p>{{ [1,2,3,4,5,6] | sum }}</p>

sort:列表排序

  <p>{{ [6,2,3,1,5,4] | sort }}</p>

语句块过滤(不常用):

  {% filter upper %}
    this is a Flask Jinja2 introduction
  {% endfilter %}
posted @ 2021-12-16 22:57  下个ID见  阅读(31)  评论(0)    收藏  举报