Creating content snippet with Jinja filter
I want to create content snippets for my home page. An example message looks something like this:
<p>Your favorite Harry Potter characters enter the Game of Thrones
universe, and you'll never guess what happens!</p>
<readmore/>
<p>...they all die</p>
On the home page I only need to appear <readmore/>
. I think I can use Beautiful Soup in a Jinja filter to strip out the readmore and all content after it. It should be clamped on the first line of a newline, or the end of a paragraph if not <readmore/>
.
How can i do this?
+3
source to share
1 answer
No need to use Beautiful Soup. Just check if there is <readmore/>
any other substring in the text and split into it, or if it hasn't been split onto a newline.
from markupsafe import Markup
@app.template_filter()
def snippet(value):
for sep in ('<readmore/>', '<br/>', '<br>', '</p>'):
if sep in value:
break
else:
sep = '\n'
return Markup(value.split(sep, 1)[0])
+4
source to share