Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

jinja2

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.

like image 760
大易归真 Avatar asked Jan 24 '26 22:01

大易归真


1 Answers

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.

like image 90
doru Avatar answered Jan 26 '26 22:01

doru