Create content snippet with Jinja filter

后端 未结 1 1797
暖寄归人
暖寄归人 2021-01-14 14:12

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 Th

相关标签:
1条回答
  • 2021-01-14 14:47

    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])
    
    0 讨论(0)
提交回复
热议问题