Django TemplateTag评估为一个布尔值 [英] Django TemplateTag evaluating to a boolean

查看:148
本文介绍了Django TemplateTag评估为一个布尔值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以创建一个评估为布尔值的Django模板标签吗?

Is it possible to create a Django template tag which evaluates to a boolean?

例如,我可以这样做:

{% if my_custom_tag %}
    ..
{% else %}
    ..
{% endif %}

此刻,我将它写成一个标签,这样做很好:

At the moment I've written it as an as tag, which works fine like this:

{% my_custom_tag as var_storing_result %}

但我只是好奇,如果我可以做到这一点,我认为如果我不必先将结果分配给一个变量,那将会更好。

But I was just curious if I could do it the other way as I think it'd be nicer if I didn't have to assign the result to a variable first.

谢谢!

推荐答案

您必须编写一个自定义的{%if%}标签有些处理这个。在我看来,最好使用你已经拥有的。它工作得很好,任何其他开发人员都很容易找出发生了什么。

You'd have to write a custom {% if %} tag of some sort to handle that. In my opinion, it's best to use what you already have in place. It works well, and is easy for any other developers to figure out what's going on.

这篇关于Django TemplateTag评估为一个布尔值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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