在Jinja2中,如果变量未定义,如何进行测试?

时间:2023-01-22 20:47:29

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

从Django转换过来,我习惯了这样做:

{% 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?

如果我没有把var1放到context中,它就会起作用。Jinja2给了我一个未定义的错误。是否有一种简单的方法来表示{% if var1 == None %}或类似?

6 个解决方案

#1


176  

From the Jinja2 template designer documentation:

来自Jinja2模板设计器文档:

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

#2


11  

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

{%如果变量被定义为%},如果变量为None,则为真。

Since not is None not is allowed, that means that

既然没有不被允许,那就意味着。

{% if variable != None %}

{% if变量!=无%}

is really your only option.

这是你唯一的选择。

#3


9  

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

在环境设置中,我们定义了undefined = StrictUndefined,它阻止了未定义的值被设置为任何值。这固定:

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

#4


7  

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

您还可以在jinja2模板中定义一个变量:

{% 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:

否则(如果您不使用{% set步骤= 1%}),上面的代码将会抛出:

UndefinedError: 'step' is undefined

#5


1  

{% 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

如果您将变量默认为False,则可以使用{%,如果不是var1 %}。

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

#6


1  

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

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

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

or something like:

或者类似的:

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

#1


176  

From the Jinja2 template designer documentation:

来自Jinja2模板设计器文档:

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

#2


11  

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

{%如果变量被定义为%},如果变量为None,则为真。

Since not is None not is allowed, that means that

既然没有不被允许,那就意味着。

{% if variable != None %}

{% if变量!=无%}

is really your only option.

这是你唯一的选择。

#3


9  

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

在环境设置中,我们定义了undefined = StrictUndefined,它阻止了未定义的值被设置为任何值。这固定:

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

#4


7  

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

您还可以在jinja2模板中定义一个变量:

{% 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:

否则(如果您不使用{% set步骤= 1%}),上面的代码将会抛出:

UndefinedError: 'step' is undefined

#5


1  

{% 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

如果您将变量默认为False,则可以使用{%,如果不是var1 %}。

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

#6


1  

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

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

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

or something like:

或者类似的:

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