Well i don’t find the answer I’m sure that it’s very simple, but i just don’t find out how to make it work like Django when it doesn’t find a variable
i tried to use Undefined and create my own undefined but it give me problems of attribute error etc.
def silently(*args, **kwargs):
return u''
class UndefinedSilently(Undefined):
__unicode__ = silently
__str__ = silently
__call__ = silently
__getattr__ = silently
but when i try this here it fails TypeError: 'unicode' object is not callable:
{%for dir_name, links in menu_links.items()%}
You are trying to go arbitrarily deep into your undefined data.
menu_linksis undefined, so Jinja2 creates a new instance of yourUndefinedSilentlyclass. It then calls the__getattr__method of this object to get theitemsattribute. This returns a blank unicode string. Which Python then tries to call (the()ofmenu_links.items()). Which raises the error that unicode objects are not callables.That is:
If you want to be able to go deeper than one level you can create a class that returns itself for every access attempt except
__str__and__unicode__.