Question

I'm using Mako templates in a Python project.

The value None sometimes comes through for a contact title. I want to hide None if the value is None.

enter image description here

Current code:

<td class="data-career request-row">
    %if 'title' in message['contact'] and 'title' is not 'None':
        ${message['contact']['title']}
    %endif
</td>

I also tried:

%if 'title' in message['contact'] and 'title' is not None:

However None still shows up, so I'm curious as to what the correct way to check for an incoming string value is in Mako?

I couldn't find anything on their docs site.

Was it helpful?

Solution

Obviously string 'title' cannot be None because it is... well, 'title'. :D

%if 'title' in message['contact'] and message['contact']['title'] is not None:
    ${message['contact']['title']}
%endif

or

%if 'title' in message['contact']:
    ${message['contact']['title'] or ''}
%endif

or simpliest/shortest

${message['contact'].get('title', None) or ''}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top