Any way to suppress key errors when a dictionary key is not found in Chameleon ZPT templates?
-
21-12-2019 - |
Question
Sometimes it would be nice to be able to do this, just return None in any expression evaluating for a nonexistent dict key instead of raising KeyError.
Solution
You'd use dict.get()
to access the key instead:
<div tal:attributes="class some_dict.get(some_key)">
where the class
attribute would be omitted if some_key
is not present, as the default return value from dict.get()
is None
if the key is missing.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow