猎豹模板过滤器

时间:2012-01-04 22:46:55

标签: python filter cheetah

很抱歉,这个问题听起来很幼稚。我有一个猎豹模板,例如:

#filter None
<html>
<body>
$none should be ''
$number should be '1'
</body>
</html>
#end filter

namespace = {'none': None, 'number': 1}
所以基本上我想将所有None和非字符串值分别转换为''和string。根据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 个答案:

答案 0 :(得分:1)

为了解释你得到的结果,让我们来定义:

def filter_to_string(value):
    if value is None:
        return ''
    return str(value)

让我们想象一下,这是我们的过滤器。现在,让我们天真地了解猎豹如何处理(它比这更多涉及)。你得到的结果将来自:

>>> "<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将是命名空间。

鉴于此,您的结果是否仍然令您惊讶?