在Mako中将字符串标记为安全

dis*_*cer 11 python pylons mako

我正在使用带有Mako模板的Pylons,我想避免一直打字:

${ h.some_function_that_outputs_html() | n }
Run Code Online (Sandbox Code Playgroud)

我想以某种方式标记函数,或者将变量标记为安全(您可以在Django中执行此操作),因此我不必一直使用pipe-en.有任何想法吗?

dis*_*cer 10

我刚刚发现如果你在你的类中放入一个html方法,那么Mako将只调用该方法并输出它在模板中返回的任何内容.

所以我做了:

def __html__(self):
    return unicode(self)
Run Code Online (Sandbox Code Playgroud)

这基本上就是h.literal的作用.