猎豹模板过滤器
抱歉如果这个问题听起来有点幼稚。我有一个Cheetah模板,比如说:
#filter None
<html>
<body>
$none should be ''
$number should be '1'
</body>
</html>
#end filter
在这个模板中,namespace = {'none': None, 'number': 1}
简单来说,我想把所有的None和非字符串的值分别转换成''(空字符串)和字符串。根据Cheetah的文档:http://www.cheetahtemplate.org/docs/users_guide_html_multipage/output.filter.html,我想要的是默认的过滤器。难道我在开头加上#filter None
就不是这样做了吗?为什么它不起作用呢?
请帮我解决这个问题。谢谢
编辑:
为了更清楚,我基本上想让它通过这个简单的if
测试:
#filter None
<html>
<body>
#if $none == '' and $number == '1':
<b>yay</b>
#end if
</body>
</html>
#end filter
所以如果一切正常,我应该只看到yay
1 个回答
1
为了让你理解你得到的结果,我们先来定义一下:
def filter_to_string(value):
if value is None:
return ''
return str(value)
假设这就是我们的过滤器。现在,我们简单地想象一下Cheetah是如何处理的(实际上要复杂一些)。你得到的结果是来自于这个:
>>> "<html>\n<body>\n" + \
filter_to_string(ns['none']) + " should be ''\n" + \
filter_to_string(ns['number']) + " should be '1'\n</body>\n</html>\n"
"<html>\n<body>\n should be ''\n1 should be '1'\n</body>\n</html>\n"
其中 ns
是命名空间的意思。
那么,考虑到这一点,你的结果还让你感到惊讶吗?