简体   繁体   English

为flask中的context_processor设置默认模板过滤器

[英]Set the default template filter for the context_processor in flask

In my flask app i have these codes:在我的烧瓶应用程序中,我有这些代码:

from __main__ import app

@app.context_processor
def breakline():
    return { 'break': '<br>' }

How to use in html format is as follows: html格式的使用方法如下:

{{ break }}

The above code works but there is a problem;上面的代码可以运行,但是有问题; A safe filter is needed to detect <br> ;需要一个safe过滤器来检测<br> ; But I do not want to be added in html format every time like this:但我不想每次都像这样以html格式添加:

{{ break | safe }}

I want this safe filter to be applied automatically and no longer need to be used in the html page.我希望自动应用这个safe过滤器,不再需要在 html 页面中使用。 Is such a thing possible?这样的事情可能吗?

This is just an example and I do not just want to be able to create a <br> .这只是一个例子,我不只是希望能够创建一个<br> I want to know if a filter can be added to a context_processor or not.我想知道是否可以将过滤器添加到 context_processor 中。

You can mark your html code as safe by using Markup .您可以使用Markup将您的 html 代码标记为安全。

from flask import Markup

@app.context_processor
def breakline():
    return { 'break': Markup('<br>') }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM