Skip to content Skip to sidebar Skip to footer

Create Content Snippet With Jinja Filter

I want to create content snippets for my home page. An example post looks something like

Your favorite Harry Potter characters enter the Game of Thrones universe, and yo

Solution 1:

There's no need to use Beautiful Soup. Just check if <readmore/> or some other substring is present in the text, and split on it, or if it's not there split on 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])

Post a Comment for "Create Content Snippet With Jinja Filter"