jinja2是否具有过滤器帮助,仅在浏览器中显示部分变量 [英] Does jinja2 have filter help show only part of variable in browser

查看:49
本文介绍了jinja2是否具有过滤器帮助,仅在浏览器中显示部分变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我使用

{{ post.body_html | safe }}

程序将从MySQL中选择数据,并在全文中显示在浏览器上. jinja2是否也具有过滤器帮助来显示变量的一部分,如文章的第一段.

program will select data from MySQL and display on browser as complete article. So does jinja2 have filter help show part of variable like article's first paragraph.

推荐答案

您可以使用truncate()过滤器.您可以将要在模板中显示的字符数作为参数发送给它:

You can use the truncate() filter. You can send to it as argument the number of characters you want to show in your template:

{{ post.body_html | truncate(40) | safe }}

很明显,您可以编写一个函数(在python文件中)以检测第一段,计算字符数,并将该数字(返回)发送到模板中的truncate()过滤器中.

Obviously you can code a function (in your python file) which detects the first paragraph, counts the number of characters and send this number (that it returns) to your truncate() filter in your template.

这篇关于jinja2是否具有过滤器帮助,仅在浏览器中显示部分变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆