使用`With`标签反转Django模板中的布尔值? [英] Use `With` Tag to invert a boolean in Django Template?

查看:150
本文介绍了使用`With`标签反转Django模板中的布尔值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想传递一个值到 include 标签,该变量是传入的变量的OPPOSITE。

I want to pass a value to an include tag that is the OPPOSITE of a variable passed in.

这是我所尝试的(基本上):

This is what I tried (basically):

{% with s_options as not disp %}
    {% include "e.html" with show_options=s_options only %}
{% endwith %}

有没有办法做我想要的?

Is there any way to do what I want?

推荐答案

不知道这是否是最好的解决方案,但我只是做了一个新的过滤器:

Not sure if this is the best solution, but I just made a new filter:

from django import template

register = template.Library()


@register.filter(name="not_value")
def not_value(true_value):
    return not true_value

然后做了:

{% load not_value %}
{% with s_options=disp|not_value %}  {# WILL NOT WORK WITH "as" #}
    {% include "e.html" with show_options=s_options only %}
{% endwith %}

请注意,po可以这样做(尽管我还没有尝试过):

Note that, possibly, this might work as well (though I have not tried):

{% include "e.html" with show_options=s_options|not_value only %}

这篇关于使用`With`标签反转Django模板中的布尔值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆