Jinja 2 safe keyword

≡放荡痞女 提交于 2019-11-29 06:28:04

问题


I have a little problem understanding what an expression like {{ something.render() | safe }} does .

From what I have seen, without the safe keyword it outputs the entire html document, not just the true content.

What I would like to know, is what it actually does, how it functions .


回答1:


The safe filter explicitly marks a string as "safe", i.e., it should not be automatically-escaped if auto-escaping is enabled.

The documentation on this filter is here.

See the section on manual escaping to see which characters qualify for escaping.




回答2:


Normally text is HTML-escaped (so <b> would be written out as &lt;b&gt;, which would render as <b>).

When you put |safe after something, you're telling the template engine that you have already escaped the text yourself, i.e. "it's safe to render this directly". So it will not do that encoding for you.

For more information: http://jinja.pocoo.org/docs/templates/#html-escaping




回答3:


For anyone coming here looking to use the safe filter programmatically: wrap it in a markupsafe.Markup class, on which Jinja2 depends on.



来源:https://stackoverflow.com/questions/12341496/jinja-2-safe-keyword

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