1

I am trying to come up with a base template for an application and one of the goals would be to remove any unnecessary js/css from pages so I want to do something in the cheetah template like

#if $dict.has_key('datepicker'):
  <link rel="stylesheet" href="$datepicker" type="text/css" />
#end if

I think this would also help with errors like namemap does not have key 'datepicker'

my current error I am getting using WSGIHandler is

TypeError: descriptor 'has_key' requires a 'dict' object but received a 'str'

I feel like this has to do with me casting the return of the handler as a str but shouldnt the template be parsed before it gets to the str

t = Template(file=WORKSPACE_PATH+"/tmpl/posts.html", searchList=[tmpldict])
self.response_body = str(t).encode('utf8')
return str(t)
BillPull
  • 6,853
  • 15
  • 60
  • 99

1 Answers1

1

The bug is this:

dict.has_key('datepicker')

"dict" is a class, so it expects the first argument of "dict.has_key" to be an instance of "dict".

You're passing a string instead of the dict object.

Basically, "d.has_key(k)" is equivalent to "dict.has_key(d, k)", and you have the latter.

MRAB
  • 20,356
  • 6
  • 40
  • 33
  • I am just confused of how to reference the dict inside the template – BillPull Jun 03 '11 at 19:42
  • After consulting the documentation, I'd say try #try...#except KeyError – MRAB Jun 03 '11 at 20:00
  • but then if the key doesnt exist I will get a KeyError rather than what I want to do is just not include that key in the parsing – BillPull Jun 03 '11 at 20:12
  • That's the point of the #try...#except KeyError: #pass, to trap and ignore the exception raised by the template line. – MRAB Jun 03 '11 at 21:54