检查tmpl_context(Python,Pylons,Genshi)中是否存在变量?

Fyn*_*ynn 6 python pylons genshi

我试图弄清楚如何使用Pylons和Python检查模板上下文'tmpl_context'中是否存在变量.我想要做的是:

我有一个Pylons布局模板.当且仅当变量c.messages存在于上下文中时,这应包含消息部分.消息部分如下所示:

<div py:if="len(c.messages) > 0">
  <py:for each="msg in c.messages">
    <strong>${msg}</strong>
  </py:for>
</div>
Run Code Online (Sandbox Code Playgroud)

如果控制器未定义c.messages,则会产生错误.所以我只想在定义变量时才包含它.有这个问题的解决方案吗?

Vin*_*cer 5

Genshi有一个明确的方法

如果定义(消息):

http://genshi.edgewall.org/wiki/Documentation/templates.html#defined-name