简体   繁体   English

在Django模板过滤器中明确设置优先级

[英]Explicitly set precedence in Django template filters

Is there a way to set explicitly set precedence in Django template filters? 有没有办法在Django模板过滤器中设置显式设置优先级?

Like a sort of brackets system. 像一种括号系统。

I'm trying to say 我想说

{{ label | default:( parameter | parse_label ) }}

A label is some human-friendly string like "Project ID" and a parameter is more like "project_id" . label是一些人性化的字符串,如"Project ID" ,参数更像是"project_id"

I want a system where if a label isn't specifically provided, it runs the parse_label custom filter which returns a human-friendly version similar to the above - but at the moment, the code I have is running the parse_label filter regardless of whether a label is provided - presumably because it's doing something like this 我想要一个系统,如果没有专门提供标签,它运行parse_label自定义过滤器,返回类似于上面的人性化版本 - 但此刻,我的代码运行parse_label过滤器,无论是否提供label - 大概是因为它正在做这样的事情

{{ (label | default:parameter) | parse_label }}

Any ideas? 有任何想法吗?

If I understand you correctly you can achieve this with the with tag. 如果我理解正确,您可以使用with标签实现此目的。

{% with param=parameter|parse_label %}
    {{ label | default:param }}
{% endwith %}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM