在 Jinja2 中,如何测试变量是否未定义?

发布于 2024-09-25 10:37:14 字数 197 浏览 8 评论 0原文

从 Django 转换后,我习惯于做这样的事情:

{% if not var1 %} {% endif %}

如果我没有将 var1 放入上下文中,它就会工作。 Jinja2 给我一个未定义的错误。有没有一种简单的方法可以表达 {% if var1 == None %} 或类似的内容?

Converting from Django, I'm used to doing something like this:

{% if not var1 %} {% endif %}

and having it work if I didn't put var1 into the context. Jinja2 gives me an undefined error. Is there an easy way to say {% if var1 == None %} or similar?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

幸福%小乖 2024-10-02 10:37:14

来自 Jinja2 模板设计器文档

{% if variable is defined %}
    value of variable: {{ variable }}
{% else %}
    variable is not defined
{% endif %}

From the Jinja2 template designer documentation:

{% if variable is defined %}
    value of variable: {{ variable }}
{% else %}
    variable is not defined
{% endif %}
太傻旳人生 2024-10-02 10:37:14

如果变量为 None,则 {% if variable is Define %} 为 true。

由于不允许 not is None ,这意味着

{% if variable != None %}

实际上是您唯一的选择。

{% if variable is defined %} is true if the variable is None.

Since not is None is not allowed, that means that

{% if variable != None %}

is really your only option.

孤独陪着我 2024-10-02 10:37:14

您还可以在 jinja2 模板中定义一个变量,如下所示:

{% if step is not defined %}
{% set step = 1 %}
{% endif %}

然后您可以像这样使用它:

{% if step == 1 %}
<div class="col-xs-3 bs-wizard-step active">
{% elif step > 1 %}
<div class="col-xs-3 bs-wizard-step complete">
{% else %}
<div class="col-xs-3 bs-wizard-step disabled">
{% endif %}

否则(如果您不使用 {% set step = 1 %}),上面的代码将扔:

UndefinedError: 'step' is undefined

You could also define a variable in a jinja2 template like this:

{% if step is not defined %}
{% set step = 1 %}
{% endif %}

And then You can use it like this:

{% if step == 1 %}
<div class="col-xs-3 bs-wizard-step active">
{% elif step > 1 %}
<div class="col-xs-3 bs-wizard-step complete">
{% else %}
<div class="col-xs-3 bs-wizard-step disabled">
{% endif %}

Otherwise (if You wouldn't use {% set step = 1 %}) the upper code would throw:

UndefinedError: 'step' is undefined
温柔一刀 2024-10-02 10:37:14

您可以使用 Jinja Elvis 运算符

{{ 'OK' if variable is defined else 'N/A' }}

或另外检查空性

{{ 'OK' if (variable is defined and variable) else 'N/A' }}

Jinja 模板 - 模板设计器文档

You can use kind of Jinja Elvis operator

{{ 'OK' if variable is defined else 'N/A' }}

or additionally check emptiness

{{ 'OK' if (variable is defined and variable) else 'N/A' }}

Jinja templates - Template Designer Documentation

つ低調成傷 2024-10-02 10:37:14

在环境设置中,我们设置了 undefined = StrictUndefined,这可以防止将未定义的值设置为任何值。这修复了它:

from jinja2 import Undefined
JINJA2_ENVIRONMENT_OPTIONS = { 'undefined' : Undefined }

In the Environment setup, we had undefined = StrictUndefined, which prevented undefined values from being set to anything. This fixed it:

from jinja2 import Undefined
JINJA2_ENVIRONMENT_OPTIONS = { 'undefined' : Undefined }
夏九 2024-10-02 10:37:14

如果您需要的话,请考虑使用默认过滤器。例如:

{% set host = jabber.host | default(default.host) -%}

或者使用更多后备值,并在末尾添加“硬编码”值,例如:

{% set connectTimeout = config.stackowerflow.connect.timeout | default(config.stackowerflow.timeout) | default(config.timeout) | default(42) -%}

Consider using default filter if it is what you need. For example:

{% set host = jabber.host | default(default.host) -%}

or use more fallback values with "hardcoded" one at the end like:

{% set connectTimeout = config.stackowerflow.connect.timeout | default(config.stackowerflow.timeout) | default(config.timeout) | default(42) -%}
无力看清 2024-10-02 10:37:14

{% if variable is Defined %} 用于检查某些内容是否未定义。

如果您将变量默认为 False,则可以使用 {% if not var1 %} 来逃脱,例如

class MainHandler(BaseHandler):
    def get(self):
        var1 = self.request.get('var1', False)

{% if variable is defined %} works to check if something is undefined.

You can get away with using {% if not var1 %} if you default your variables to False eg

class MainHandler(BaseHandler):
    def get(self):
        var1 = self.request.get('var1', False)
划一舟意中人 2024-10-02 10:37:14

我在 Ansible 中遇到了这样的问题。最终必须对 @Garret 和 @Carsten / @azalea 答案进行测试,所以:

{% if variable is defined and variable %}
    value of variable: {{ variable }}
{% else %}
    variable is not defined or is falsy
{% endif %}

I had an issue like this in Ansible. Ended up having to do a test on both @Garret and @Carsten / @azalea answers, so:

{% if variable is defined and variable %}
    value of variable: {{ variable }}
{% else %}
    variable is not defined or is falsy
{% endif %}
把时间冻结 2024-10-02 10:37:14

你可以这样做:

- debug: 
    msg: "Executed only if variable is defined"
  when:
    - variable is defined

You can do this :

- debug: 
    msg: "Executed only if variable is defined"
  when:
    - variable is defined
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文