1

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

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

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

所以如果一切顺利,我应该看到的是

4

1 回答 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

鉴于此,您的结果是否仍然让您感到惊讶?

于 2012-01-04T23:53:20.057 回答