我在Python项目中使用了Mako模板。
有时会为联系人标题使用值None。如果值是None
,我想隐藏“无”。
当前代码:
<td class="data-career request-row">
%if 'title' in message['contact'] and 'title' is not 'None':
${message['contact']['title']}
%endif
</td>
我也尝试过:
%if 'title' in message['contact'] and 'title' is not None:
但是仍然没有显示,所以我很好奇在Mako中检查传入字符串值的正确方法是什么?
我在他们的docs site上找不到任何东西。
最佳答案
显然,字符串'title'
不能为None
,因为它是... 'title'
。 :D
%if 'title' in message['contact'] and message['contact']['title'] is not None:
${message['contact']['title']}
%endif
要么
%if 'title' in message['contact']:
${message['contact']['title'] or ''}
%endif
或最简单/最短
${message['contact'].get('title', None) or ''}
关于python - Python-Mako模板:如何检查传入的字符串?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18991194/