Does jinja2 have filter help show only part of variable in browser

1.6k views Asked by At

If I use

{{ post.body_html | safe }}

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.

1

There are 1 answers

0
doru On

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 }}

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.