How to not render a entire string with jinja2

家住魔仙堡 提交于 2021-01-19 06:08:25

问题


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?


回答1:


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>


来源:https://stackoverflow.com/questions/36458482/how-to-not-render-a-entire-string-with-jinja2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!