1

Sorry if the question sounds naive. I have a cheetah template, e.g:

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

with namespace = {'none': None, 'number': 1}
So basically I want to convert all None and non-string values to '' and string respectively. According to Cheetah's doc: http://www.cheetahtemplate.org/docs/users_guide_html_multipage/output.filter.html , what I want is the default filter. Isn't that what I did by putting #filter None at the beginning? How come it doesn't work?

Please help me get this right. Thanks

EDIT:
To make it clearer, basically I want it to pass this simple if test:

#filter None
<html>
<body>
#if $none == '' and $number == '1':
<b>yay</b>
#end if
</body>
</html>
#end filter

So if it works out all i should see is yay

BPm
  • 2,924
  • 11
  • 33
  • 51
  • But... what's the result you're getting instead? – Ricardo Cárdenes Jan 04 '12 at 23:14
  • @RicardoCárdenes what i got is $none == None and $number == 1. Not string – BPm Jan 04 '12 at 23:23
  • I just run your example and got ` should be ''` and `1 should be '1'`, which is what I'd expect. I take that you want `'' should be ''` and `'1' should be '1'`? – Ricardo Cárdenes Jan 04 '12 at 23:28
  • yeah, that's what i want :) I mean they should be converted to string. If i do `#if $none == '' and $number == '1':

    yay

    #end if` but it doesn't pass that `if` test
    – BPm Jan 04 '12 at 23:37
  • 1
    well, `None` *is* being translated as `''`, and the same for `number`. But you won't see quotes around them; that would be quite useless for normal templating. Also, the actual values are kept as they are: the filtering is performed when substituting the values into the template, which is why your test doesn't pass! – Ricardo Cárdenes Jan 04 '12 at 23:46
  • Check the answer. It explains a little bit more what is going on. – Ricardo Cárdenes Jan 04 '12 at 23:53

1 Answers1

1

To explain the results you're getting, let's define this:

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

Let's imagine that's our filter. Now, let's be naïve about how Cheetah does the processing (it's a bit more involved than this). The result you're getting would come from this:

>>> "<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"

where ns would be the namespace.

Given that, does your result still surprises you?

Ricardo Cárdenes
  • 9,004
  • 1
  • 21
  • 34