Pystache 无需转义(未转义)
Pystache without escaping (unescaped)
我正在使用 pystache 呈现模板。当我呈现具有 & 的上下文变量时,我在输出中得到 &
。如何摆脱 &
我需要 & 的地方。
Django 模板也发生了同样的事情
>>> pystache.render('The URL {{URL}}', {'URL': 'http://google.com?a=3&b=3'})
u'The URL http://google.com?a=3&b=3'
要防止转义,请使用三重大括号 {{{var}}}
为防止转义,请使用三重大括号,{{{URL}}}
而不是双大括号 {{URL}}
>>> pystache.render('The URL {{{URL}}}', {'URL': 'http://google.com?a=3&b=3'})
u'The URL http://google.com?a=3&b=3'
我已经在今天的最新版本 0.5.4
上对此进行了测试
小胡子文档
由于 Pystache 是 Python 中的 Mustache 实现,您可以使用 Mustache 的文档作为指导。
All variables are HTML escaped by default. If you want to return
unescaped HTML, use the triple mustache: {{{name}}}.
很久以前they've got such suggestion.
有辅助escape
选项 Renderer
class初始化器。此选项接受对字符串进行操作的函数。默认值为 cgi.escape(s, quote=True)
。
所以当你写的时候:
import pystache
rend = pystache.Rendered(escape=lambda s: s)
rend.render(your_obj)
您在模板中得到了没有三括号的未转换值。
参见 Rendered
docs class
我正在使用 pystache 呈现模板。当我呈现具有 & 的上下文变量时,我在输出中得到 &
。如何摆脱 &
我需要 & 的地方。
Django 模板也发生了同样的事情
>>> pystache.render('The URL {{URL}}', {'URL': 'http://google.com?a=3&b=3'})
u'The URL http://google.com?a=3&b=3'
要防止转义,请使用三重大括号 {{{var}}}
为防止转义,请使用三重大括号,{{{URL}}}
而不是双大括号 {{URL}}
>>> pystache.render('The URL {{{URL}}}', {'URL': 'http://google.com?a=3&b=3'})
u'The URL http://google.com?a=3&b=3'
我已经在今天的最新版本 0.5.4
小胡子文档
由于 Pystache 是 Python 中的 Mustache 实现,您可以使用 Mustache 的文档作为指导。
All variables are HTML escaped by default. If you want to return unescaped HTML, use the triple mustache: {{{name}}}.
很久以前they've got such suggestion.
有辅助escape
选项 Renderer
class初始化器。此选项接受对字符串进行操作的函数。默认值为 cgi.escape(s, quote=True)
。
所以当你写的时候:
import pystache
rend = pystache.Rendered(escape=lambda s: s)
rend.render(your_obj)
您在模板中得到了没有三括号的未转换值。
参见 Rendered
docs class