Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to not render a entire string with jinja2

I'm building a blog from start for a homework assignment in Google App Engine in python and I'm using jinja2 to render my html. My problem is that like every blog when an entry is too long; the blog just renders a part of the entry in the main page. I want to do that, when the main page is rendered I took the post from the database and pasted it to jinja. Are there any filters or functions to tell jinja, for example, this string can not be longer than x number?

like image 479
Ordani Sanchez Avatar asked Nov 28 '22 16:11

Ordani Sanchez


1 Answers

Look at docs

Jinja2 has truncate filter truncate(s, length=255, killwords=False, end='...'). Example usage

<div>{{ blogpost.text|truncate }}</div>

Or

<div>{{ blogpost.text|truncate(1024, True) }}</div>
like image 192
vitalii Avatar answered Dec 05 '22 13:12

vitalii