pylons mako how to check if variable exist or not

mako, pylons

Solution

From the mako Docs on Context Variables:

% if someval is UNDEFINED:
    someval is: no value
% else:
    someval is: ${someval}
% endif

The docs describe this as referencing variable names not in the current context. Mako will set these variables to the value `UNDEFINED`.

I check for variables like so:

% if not someval is UNDEFINED:
    (safe to use someval)

However, if pylons/pyramid has `strict_undefined=True` setting, attempts to use the undefined variable results in a `NameError` being raised. They give a brief philisophical justification for doing it this way, instead of simply replacing un-set variables with empty strings which seems consistent with Python philosophy. Took me a while to find this, but reading that entire section on the Mako Runtime will clear up how Mako recieves, sets, and uses context variables.

Edit: For completions sake, the documents explain the `strict_undefined` setting here. You can change this variable by setting it in one of your .ini files:

[app:main]
...
mako.strict_undefined = false

Problem

In django, we can do this: ``` views.py : def A(request): context = {test : 'test'} return render_to_response('index.html', context , context_instance = RequestContext(request)) def B(request): context = {} return render_to_response('index.html', context , context_instance = RequestContext(request)) index.html: {% if test %} {{ test }} {% endif %} ``` And have our template render without error, even if i use `method B`, where variable `'test'` does not exist, but I still can put it in the template. I want to do the same with pylons + mako, in controller : ``` foo.py def A(self): c.test = 'test' return render('index.html') def B(self): return render('index.html') index.html : % if c.test: ${'c.test'} % endif ``` In Django, I can do that, but in Pylons, I get an error, is there anyway to check wheter `'c.test'` exists or not? the error : AttributeError: 'ContextObj' object has no attribute 'test'

Original source